【发布时间】:2016-08-02 06:19:22
【问题描述】:
如果我从 WotClass.h 中注释掉 #define 行,则会出现编译错误:WotClass.cpp:7: error: 'BFLM_DEFINE' was not declared in this scope
它不应该是范围独立的吗?还是顺序有问题?
WotClass.h
#ifndef WOTCLASS_H
#define WOTCLASS_H
#define BFLM_DEFINE 1 // if commented out then compile fails.
class WotClass{
public:
WotClass();
int foo();
private:
};
#endif
WotClass.cpp
#include "WotClass.h"
WotClass::WotClass(){}
int WotClass::foo(){
return BFLM_DEFINE;
}
Test.ino
#define BFLM_DEFINE 1 // This is before including class
#include "WotClass.h"
void setup(){
Serial.begin(115200);
Serial.println(BFLM_DEFINE);
WotClass x;
Serial.print(x.foo());
}
void loop(){}
【问题讨论】:
-
从 Test 中删除定义,并将其包含在头文件 WotClass.h 中。 cpp 只包含头部,没有定义,因此失败。