如果我注释掉WotClass.h中的#define行,我得到了编译错误:WotClass.cpp:7: error: 'BFLM_DEFINE' was not declared in this scope
#define不会从主程序传播到class.cpp?
是不是应该是范围独立的?或者是订单中的问题?
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中删除define,并将其包含在头文件WotClass.h中。 cpp只包含标题,它没有定义,因此失败。 – RvdK