如果我註釋掉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