给出以下代码:
// 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'
答案 0 :(得分:1)
moc_other_file.cpp
不是other_file.cpp
。因此,您在BIG_DATA_MACRO
中定义other_file.cpp
的事实对moc_other_file.cpp
没有影响。
尽管如此,你仍然错过了TEXT
的定义。你只需要添加它。
作为一个观点,TEXT
是一个相当糟糕的名字;大写标识符通常用于宏。 TEXT
特别是Windows.h
中使用的宏。