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

Flo*_*33R 1 c++

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

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

E0020标识符“测试”未定义

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;
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

MeasureWeather.cpp:

include "MeasureWeather.h"

void MeasureWeather::setSensorPin(uint8_t pin) 
{   
    _sensorPin = pin; 
}
Run Code Online (Sandbox Code Playgroud)

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

Kei*_*son 5

您可以将它放在头文件的顶部,作为include保护的一部分:

#define MeasureWeather
Run Code Online (Sandbox Code Playgroud)

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

MeasureWeather test;
Run Code Online (Sandbox Code Playgroud)

扩展到

 test;
Run Code Online (Sandbox Code Playgroud)

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

#include警卫使用其他标识符。

  • 一个很好的例子:“看,这就是为什么您的问题中需要MCVE”。 (3认同)