在 Foo.h 中:
class Foo
{
public:
Foo();
static const unsigned int FOOBAR = 10;
static const unsigned int BARFOO = 20;
private:
unsigned int m_FooBar;
bool m_Bar;
void Bar();
};
在 Foo.cpp 中:
Foo::Foo()
: m_FooBar(FOOBAR), // this works
m_Bar(false)
{
}
void Foo::Bar()
{
//m_FooBar = m_Bar ? FOOBAR : BARFOO; // linker fails *1
m_FooBar = FOOBAR; // ok
}
我正在使用 GCC 4.5.3 进行编译。取消注释行 *1 时链接器会失败的原因是什么?
Foo.o: In function 'Foo::Bar' (name unmangled):
Foo.cpp: undefined reference to `Foo::FOOBAR'
Foo.cpp: undefined reference to `Foo::BARFOO'
尝试使用 VC2005、2008、2010 和 CB2010。他们都编译并链接得很好。为什么 GCC 在这种情况下会失败?
鉴于 answer here ,为什么其他流行的编译器不像 GCC 那样失败?不管怎样,对于 GCC 或其他流行的编译器来说,它必须是一个错误。还是有更合理的解释?
最佳答案
形式上,头文件只声明了静态常量,而且它们也必须被定义(至少在 C++03 中是这样)。但是,如果您只使用它们的值,那么您通常会逃脱惩罚。
在 C++11 中,这被更正式地指定为当静态“使用 odr”时需要定义。 *1
行就是一个例子。三元运算符试图形成对值的引用,而编译器(或实际上是链接器)意识到它不能。
C++11 标准说
9.4.2 Static data members
§3...
The member shall still be defined in a namespace scope if it is odr-used (3.2) in the program and the namespace scope definition shall not contain an initializer.
关于c++ - 静态常量类成员声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12489597/