标识符“ test”未定义;在这种情况下这意味着什么

时间:2019-10-21 19:04:20

标签: c++

我正在为一个覆盆子写一个气象站。我总是收到错误消息标识符“ test”未定义。

我已经尝试通过一个小示例不使用任何外部类,并且它运行良好。现在,我正在尝试创建一个对象测试,但这是行不通的。我总是收到错误消息:

E0020标识符“ test”未定义

main.cpp:

#include <wiringPi.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <errno.h>
#include <stdlib.h>

include "MeasureWeather.h"
int main(void)
{
    MeasureWeather test;

while (1)
{
    test._setSensorPin(DHT_PIN);
}

return 0;
}

MeasureWeather.h:

#ifndef MeasureWeather
#define MeasureWeather

#include <wiringPi.h>
#include <stdio.h>
#include <iostream>
#include <errno.h>
#include <stdlib.h>
#include <cstring>
class MeasureWeather
{
public:

//setter for sensor pin
void _setSensorPin(uint8_t pin);


private:
    uint8_t sensorPin;
};

#endif // !MeasureWeather

MeasureWeather.cpp:

include "MeasureWeather.h"

void MeasureWeather::setSensorPin(uint8_t pin) 
{   
    _sensorPin = pin; 
}

我希望有人可以帮助我解决我的问题。谢谢!

1 个答案:

答案 0 :(得分:5)

您将其放在头文件的顶部,作为包含保护的一部分:

#define MeasureWeather

MeasureWeather是课程的名称。通过将其定义为宏,可以隐藏类名称。因此,线

MeasureWeather test;

扩展到

 test;

这将是对称为test的东西的引用,而不是声明。

为您的#include后卫使用其他标识符。