Qt moc文件没有扩展在#include“foo.h”之前定义的宏。

时间:2016-10-24 11:23:51

标签: c++ qt moc

给出以下代码:

// foo.h
#ifdef BIG_DATA_MACRO
    #warning "TEXT ADDED"
    #define TEXT_HANDLING_MACRO  \
         static const char * TEXT[]; \
         static const char * getText( int _enum ) { \
             return TEXT[_enum]; \
         }
#else
    #warning "TEXT NOT ADDED"
    #define TEXT_HANDLING_MACRO
#endif

struct Foo {
    TEXT_HANDLING_MACRO
};
// foo.cpp
#include "foo.h"
#ifdef BIG_DATA_MACRO
    const char * Foo::TEXT[] = { 
        "ONE",
        "TWO",
        "THREE",
        0
   };
 #endif
// other_file.cpp
#include <iostream>
#define BIG_DATA_MACRO  
#include "foo.h"
void bar() {
    std::cout << Foo::TEXT[0] <<std::endl;
}

警告TEXT ADDED随处可见,但TEXT NOT ADDED显示moc_other_file.cpp。我们如何解决这个问题。 编译输出是:

  

/foo.h:15:警告:#warning“TEXT NOT ADDED”[ - WCpp] Debug / moc_other_file.cpp:9:from moc_otherfile.cpp:9:

     

other_file.cpp:26:错误:未定义引用'Foo :: TEXT'

1 个答案:

答案 0 :(得分:1)

顾名思义,

moc_other_file.cpp不是other_file.cpp。因此,您在BIG_DATA_MACRO中定义other_file.cpp的事实对moc_other_file.cpp没有影响。

尽管如此,你仍然错过了TEXT的定义。你只需要添加它。

作为一个观点,TEXT是一个相当糟糕的名字;大写标识符通常用于宏。 TEXT特别是Windows.h中使用的宏。