我试图用C ++写一个基本的气象监测站,我有2个传感器写成类,我试图做一个基本程序来实现这些类和测试它们是否按预期运行。当我尝试构建项目时,Eclipse回复时出现以下错误消息,这使我认为我没有正确地包含我的头文件或其他内容。如果有人发现错误,请告诉我,谢谢!
./src/HelloWorld.o: In function `main':
/Debug/../src/HelloWorld.cpp:30: undefined reference to `pressureSensor::pressureSensor()'
/Debug/../src/HelloWorld.cpp:31: undefined reference to `temperatureSensor::temperatureSensor()'
/Debug/../src/HelloWorld.cpp:34: undefined reference to `pressureSensor::getReading()'
/Debug/../src/HelloWorld.cpp:31: undefined reference to `temperatureSensor::~temperatureSensor()'
/Debug/../src/HelloWorld.cpp:41: undefined reference to `pressureSensor::~pressureSensor()'
/Debug/../src/HelloWorld.cpp:31: undefined reference to `temperatureSensor::~temperatureSensor()'
/Debug/../src/HelloWorld.cpp:41: undefined reference to `pressureSensor::~pressureSensor()'
collect2: error: ld returned 1 exit status
这是项目的目录结构:
/workspaces
- /src
- /HelloWorld.cpp
- /Sensors
- /temperature.h
- /temperature.cpp (compiles correctly and has entries for all member classes)
- /pressure.h
- /pressure.cpp (compiles correctly and has entries for all member classes)
以下是有问题的文件:
/src/HelloWorld.cpp
#include <iostream>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <cerrno>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include "../Sensors/pressure.h"
#include "../Sensors/temperature.h"
using namespace std;
int main(void) {
if (wiringPiSetup()) {
cout << "wiringPi is tangled, exiting." << endl;
} else {
cout << "wiringPi is untangled and ready." << endl;
}
pinMode(0, INPUT);
pinMode(1, INPUT);
pullUpDnControl(0, PUD_UP);
pullUpDnControl(1, PUD_UP);
pressureSensor pressure;
temperatureSensor temperature;
for (int i = 0; i < 5; i++) {
cout << pressure.getReading() << endl;
cout << "GPIO 17: " << digitalRead(0) << endl;
cout << "GPIO 18: " << digitalRead(1) << endl;
cout << endl;
usleep(150000);
}
return 0;
}
/Sensors/pressure.h
#ifndef PRESSURE_H_
#define PRESSURE_H_
#include "../BMP180/BMP180.h"
class pressureSensor {
public:
pressureSensor();
~pressureSensor();
int getReading();
private:
SFE_BMP180 pressure;
char status;
double T,P,p0,a;
};
#endif /* PRESSURE_H_ */
/Sensors/temperature.h
#ifndef TEMPERATURE_H_
#define TEMPERATURE_H_
#define TMP102_ADDR 0x48
#define TMP102_CFGPTR1 0x01
#define TMP102_CFGREG1 0x61
#define TMP102_CFGREG2 0xA0
#define TMP102_CFGREGSO 0xE1
class temperatureSensor {
public:
temperatureSensor();
~temperatureSensor();
float getReading();
private:
int TMPdevID;
};
#endif /* TEMPERATURE_H_ */