如果我注释掉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(){}
考虑编译WtoClass.cpp
:
-
首先,预处理器引入
WotClass.h
。由于您注释掉了#define
,这意味着WotClass.h
没有定义BFLM_DEFINE
。 -
不确定什么是
Test.ino
,但至少从您的代码来看,它与WotClass.cpp
的编译无关。
因此,在编译这个源代码时,BFLM_DEFINE
确实是未定义的。它可能是在其他源文件中定义的,但这与这个编译单元无关。这正是编译器告诉你的:
WotClass.cpp:7: error: 'BFLM_DEFINE' was not declared in this scope
WotClass.cpp
编译失败。在编译该文件时,编译器只能从WotClass.h
标头中获得BFLM_DEFINE
标识符。如果没有在那里定义,编译就会失败。