简体   繁体   中英

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?

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.

You probably want:

char[] debug = "I got here";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM