Martin Rasmussen
Martin Rasmussen

Reputation: 27

Using method from class in another .h file in c++

I'm new in C++, and have been given a (relatively) complex piece of code.

I want to call a function transmit() from a .h file in another .c file. The transmit() is in the file serviceUart.hpp which content looks like this:

serviceUart.hpp

class ServiceUart
{
public:
    ServiceUart();
  void ioConfig(); // sets io HW ports and pins. Only needed at first boot
    void ioInit(); //
    bool readTrigger();
    bool detectConnection() {return (m_rxPin.get()|| m_enabled);}
    bool startup();
    bool transmit(const char* s, uint16_t length, bool wait = false);

The file drvr.cpp is where I try calling the function. A snippet of what I think is relevant from that file looks like this:

drvr.cpp

#include "EHS5_drv.hpp"

char debug[] = "I got to here!";
transmit(debug,true);

I tried serviceUart.transmit and serviceUart::transmit, but no matter what I try, I get the error code `#20 identifier "transmit" is undefined". I guess I'm misunderstanding the syntax?

Upvotes: 1

Views: 156

Answers (2)

Gor Asatryan
Gor Asatryan

Reputation: 924

Method bool transmit(const char* s, uint16_t length, bool wait = false); is defined in ServiceUart. You have to create an object of ServiceUart class and then call method transmit()

char debug[] = "I got to here!";

ServiceUart obj;
obj.transmit(debug,true);

Or with new

ServiceUart* obj = new ServiceUart();
obj->transmit(debug,true);
delete obj;

Don't forget to delete obj.

Upvotes: 2

Hemil
Hemil

Reputation: 1016

You probably want:

char[] debug = "I got here";

ServiceUart serviceObg;
obj.transmit(debug, true);

Upvotes: 2

Related Questions