簡體   English   中英

在C ++中的另一個.h文件中使用類中的方法

[英]Using method from class in another .h file in c++

我是C ++的新手,並且獲得了(相對)復雜的代碼。

我想從另一個.c文件中的.h文件調用函數transmit() sendp transmit()位於文件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);

文件drvr.cpp是我嘗試調用該函數的地方。 我認為與該文件相關的代碼片段如下所示:

drvr.cpp

#include "EHS5_drv.hpp"

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

我嘗試了serviceUart.transmit和serviceUart :: transmit,但是無論我如何嘗試,我都會得到錯誤代碼`#20標識符“ transmission”未定義”。我猜我對語法有誤解嗎?

方法bool transmit(const char* s, uint16_t length, bool wait = false); ServiceUart定義。 您必須創建一個ServiceUart類的對象,然后調用方法transmit()

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

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

或與new

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

不要忘記刪除obj。

您可能想要:

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