简体   繁体   English

在C ++中的另一个.h文件中使用类中的方法

[英]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. 我是C ++的新手,并且获得了(相对)复杂的代码。

I want to call a function transmit() from a .h file in another .c file. 我想从另一个.c文件中的.h文件调用函数transmit() The transmit() is in the file serviceUart.hpp which content looks like this: sendp transmit()位于文件serviceUart.hpp中,其内容如下所示:

serviceUart.hpp 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. 文件drvr.cpp是我尝试调用该函数的地方。 A snippet of what I think is relevant from that file looks like this: 我认为与该文件相关的代码片段如下所示:

drvr.cpp 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? 我尝试了serviceUart.transmit和serviceUart :: transmit,但是无论我如何尝试,我都会得到错误代码`#20标识符“ transmission”未定义”。我猜我对语法有误解吗?

Method bool transmit(const char* s, uint16_t length, bool wait = false); 方法bool transmit(const char* s, uint16_t length, bool wait = false); is defined in ServiceUart . ServiceUart定义。 You have to create an object of ServiceUart class and then call method transmit() 您必须创建一个ServiceUart类的对象,然后调用方法transmit()

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

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

Or with new 或与new

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

Don't forget to delete obj. 不要忘记删除obj。

You probably want: 您可能想要:

char[] debug = "I got here";

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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