-1
我有一個類,它處理我的程序稱爲「serial.h和serial.cpp」的串行通信。它具有以下構造:未定義的引用類::對象
#include "serial.h"
serialib LS;
serial::serial(void)
{
int Ret;
Ret = LS.Open(DEVICE_PORT, BAUD_RATE);
if (Ret != 1)
{
printf("Serial port open FAILED!\n");
}
else
{
printf("Serial port successfully opened...");
}
}
我想調用這個類中的另一個和使用它的方法,所以我在一個叫dataHandler.cpp類以下內容:
#include "dataHandler.h"
#include "serial.h"
using namespace opendnp3;
serial ser;
dataHandler::dataHandler(void)
{
}
dataHandler::~dataHandler(void)
{
}
int dataHandler::sendRestartCommand()
{
int Ret;
char buffer[128];
RestartInfo ri;
std::string strW = "GetRestartInfo\r\n";
std::string strR;
Ret = ser.Write(strW);
int bytes;
Ret = ser.Read(strR);
if ((strR.compare("201-OK [GetRestartInfo]\r\n")) != 0)
{
printf ("Wrong response from device to restart message.\n");
return 0;
}
Ret = ser.Read(strR);
std::string s_bytes = strR.substr(4,3);
std::stringstream ss(s_bytes);
if (!(ss >> bytes))
bytes = 0;
Ret = ser.Read(buffer);
writeSettings(ri);
return 1;
}
然而,當我做到這一點,我得到以下錯誤:
dataHandler.o: In function `dataHandler::sendRestartCommand()':
dataHandler.cpp:(.text+0x31c): undefined reference to `dataHandler::ser'
collect2: error: ld returned 1 exit status
我原來的計劃是在我喜歡的.h文件來創建串行對象:
public:
serial ser;
但是,這也沒有工作... 我有點難住至於如何做到這一點,我知道我可能會錯過一些小東西。 有什麼建議嗎?
不,不...首先,如果您使用全局變量,則違反了封裝規則。其次,串行類的聲明在哪裏 - 這可能是你的問題。最後,請重構您的代碼並嘗試使用OOP規則:http://stackoverflow.com/questions/399656/are-there-any-rules-for-oop – LukeCodeBaker
您至少應該包含標題。或者更好提供一個[SSCCE](http://sscce.org) –
謝謝,我會研究它,需要在面向對象 –