PlatformIO 对已定义属性的未识别引用错误
PlatformIO Unidentified reference error to defined attributes
我正在使用 PlatformIO 和 Nodemcuv2 微控制器创建一个项目。
我写了一个class用于串口通信SerialCommunicationHandler
。这个 class ICommunicationHandler
实现了一个接口。请参阅下面的代码。
ICommunicationHandler.h
class ICommunicationHandler {
public:
virtual void sendTemperature(float temp) = 0;
virtual void receiveData() = 0;
virtual void update() = 0;
protected:
virtual void parseData() = 0;
virtual void showParsedData() = 0;
};
SerialCommunicationHandler
头文件
#include "ICommunicationHandler.h"
class SerialCommunicationHandler : public ICommunicationHandler {
private:
//atributes needed for storing and modifying incoming data.
static char incomingData[6]; //char array to temporarily store incoming data.
static char receivedData[6]; //char array to copy incoming data to.
static unsigned int messagePosition; //index of the incomingData array.
bool receiving;
bool newData;
void parseData() override;
void receiveData() override;
void showParsedData() override;
public:
explicit SerialCommunicationHandler();
void sendTemperature(float temp) override;
void update() override;
};
.cpp
#include <Arduino.h>
#include "SerialCommunicationHandler.h"
SerialCommunicationHandler::SerialCommunicationHandler() {
messagePosition = 0;
receiving = false;
newData = false;
}
void SerialCommunicationHandler::receiveData() {
//check if there are bytes in the serial buffer.
while (Serial.available() > 0){
char inByte = Serial.read();
//check if the byte is a starting or ending character;
switch (inByte) {
case '<':
//start receiving characters
receiving = true;
break;
case '>':
//stop receiving and parse the incoming data.
receiving = false;
newData = true;
strcpy(receivedData, incomingData); //copy incoming data into receivedData for further parsing.
memset(incomingData, 0, sizeof(incomingData)); //resetting incomingData.
messagePosition = 0;
break;
default:
if (receiving) {
incomingData[messagePosition] = inByte; //add incoming byte to array.
messagePosition++;
}
break;
}
}
}
void SerialCommunicationHandler::parseData() {
if (newData) {
showParsedData();
}
newData = false;
}
void SerialCommunicationHandler::showParsedData() {
Serial.println(receivedData);
}
void SerialCommunicationHandler::sendTemperature(float temp) {
Serial.println(temp);
}
void SerialCommunicationHandler::update() {
receiveData();
parseData();
}
构建时出现多个未定义的引用错误:
*/ld.exe: .pio\build\nodemcuv2\src\SerialCommunicationHandler.cpp.o:(.text._ZN26SerialCommunicationHandler14s
howParsedDataEv+0x0): undefined reference to `_ZN26SerialCommunicationHandler12receivedDataE`
*/ld.exe: .pio\build\nodemcuv2\src\SerialCommunicationHandler.cpp.o:(.text._ZN26SerialCommunicationHandlerC2E
v+0x4): undefined reference to `_ZN26SerialCommunicationHandler15messagePositionE'
*/ld.exe: .pio\build\nodemcuv2\src\SerialCommunicationHandler.cpp.o:(.text._ZN26SerialCommunicationHandler11r
eceiveDataEv+0x0): undefined reference to `_ZN26SerialCommunicationHandler12incomingDataE'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\nodemcuv2\firmware.elf] Error 1
我已多次检查我的代码是否存在语法错误或拼写错误,但一无所获。我的 IDE 也没有出现任何错误。欢迎提供有关可能导致未定义引用错误的任何信息。
头文件
#include "ICommunicationHandler.h"
class SerialCommunicationHandler : public ICommunicationHandler {
private:
//atributes needed for storing and modifying incoming data.
static char incomingData[6]; //char array to temporarily store incoming data.
static char receivedData[6]; //char array to copy incoming data to.
static unsigned int messagePosition; //index of the incomingData array.
// ... etc
这些只是静态成员变量的声明。您还必须在 cpp 文件中定义它们,就像您对成员函数所做的那样:
.cpp 文件:
void SerialCommunicationHandler::showParsedData() {
Serial.println(receivedData);
}
// static data members
char SerialCommunicationHandler::incomingData[6] = {};
char SerialCommunicationHandler::receivedData[6] = {};
unsigned int SerialCommunicationHandler::messagePosition = 0;
我正在使用 PlatformIO 和 Nodemcuv2 微控制器创建一个项目。
我写了一个class用于串口通信SerialCommunicationHandler
。这个 class ICommunicationHandler
实现了一个接口。请参阅下面的代码。
ICommunicationHandler.h
class ICommunicationHandler {
public:
virtual void sendTemperature(float temp) = 0;
virtual void receiveData() = 0;
virtual void update() = 0;
protected:
virtual void parseData() = 0;
virtual void showParsedData() = 0;
};
SerialCommunicationHandler
头文件
#include "ICommunicationHandler.h"
class SerialCommunicationHandler : public ICommunicationHandler {
private:
//atributes needed for storing and modifying incoming data.
static char incomingData[6]; //char array to temporarily store incoming data.
static char receivedData[6]; //char array to copy incoming data to.
static unsigned int messagePosition; //index of the incomingData array.
bool receiving;
bool newData;
void parseData() override;
void receiveData() override;
void showParsedData() override;
public:
explicit SerialCommunicationHandler();
void sendTemperature(float temp) override;
void update() override;
};
.cpp
#include <Arduino.h>
#include "SerialCommunicationHandler.h"
SerialCommunicationHandler::SerialCommunicationHandler() {
messagePosition = 0;
receiving = false;
newData = false;
}
void SerialCommunicationHandler::receiveData() {
//check if there are bytes in the serial buffer.
while (Serial.available() > 0){
char inByte = Serial.read();
//check if the byte is a starting or ending character;
switch (inByte) {
case '<':
//start receiving characters
receiving = true;
break;
case '>':
//stop receiving and parse the incoming data.
receiving = false;
newData = true;
strcpy(receivedData, incomingData); //copy incoming data into receivedData for further parsing.
memset(incomingData, 0, sizeof(incomingData)); //resetting incomingData.
messagePosition = 0;
break;
default:
if (receiving) {
incomingData[messagePosition] = inByte; //add incoming byte to array.
messagePosition++;
}
break;
}
}
}
void SerialCommunicationHandler::parseData() {
if (newData) {
showParsedData();
}
newData = false;
}
void SerialCommunicationHandler::showParsedData() {
Serial.println(receivedData);
}
void SerialCommunicationHandler::sendTemperature(float temp) {
Serial.println(temp);
}
void SerialCommunicationHandler::update() {
receiveData();
parseData();
}
构建时出现多个未定义的引用错误:
*/ld.exe: .pio\build\nodemcuv2\src\SerialCommunicationHandler.cpp.o:(.text._ZN26SerialCommunicationHandler14s
howParsedDataEv+0x0): undefined reference to `_ZN26SerialCommunicationHandler12receivedDataE`
*/ld.exe: .pio\build\nodemcuv2\src\SerialCommunicationHandler.cpp.o:(.text._ZN26SerialCommunicationHandlerC2E
v+0x4): undefined reference to `_ZN26SerialCommunicationHandler15messagePositionE'
*/ld.exe: .pio\build\nodemcuv2\src\SerialCommunicationHandler.cpp.o:(.text._ZN26SerialCommunicationHandler11r
eceiveDataEv+0x0): undefined reference to `_ZN26SerialCommunicationHandler12incomingDataE'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\nodemcuv2\firmware.elf] Error 1
我已多次检查我的代码是否存在语法错误或拼写错误,但一无所获。我的 IDE 也没有出现任何错误。欢迎提供有关可能导致未定义引用错误的任何信息。
头文件
#include "ICommunicationHandler.h"
class SerialCommunicationHandler : public ICommunicationHandler {
private:
//atributes needed for storing and modifying incoming data.
static char incomingData[6]; //char array to temporarily store incoming data.
static char receivedData[6]; //char array to copy incoming data to.
static unsigned int messagePosition; //index of the incomingData array.
// ... etc
这些只是静态成员变量的声明。您还必须在 cpp 文件中定义它们,就像您对成员函数所做的那样:
.cpp 文件:
void SerialCommunicationHandler::showParsedData() {
Serial.println(receivedData);
}
// static data members
char SerialCommunicationHandler::incomingData[6] = {};
char SerialCommunicationHandler::receivedData[6] = {};
unsigned int SerialCommunicationHandler::messagePosition = 0;