#define不会从主程序传播到class.cpp?

Com*_*tix 0 c++ class arduino

如果我从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
Run Code Online (Sandbox Code Playgroud)

WotClass.cpp

#include "WotClass.h"

WotClass::WotClass(){}

int WotClass::foo(){
    return BFLM_DEFINE;
}
Run Code Online (Sandbox Code Playgroud)

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

Ami*_*ory 5

考虑编译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
Run Code Online (Sandbox Code Playgroud)