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那样失败?无论如何,它必须是一个bug,无论是GCC还是其他流行的编译器。还是有更合理的解释?
嗯...它在VS2010编译得很好。 – Mysticial
编译和链接CB2010和VS2005 –
@Mysticial问题是关于gcc,但。我可以确认链接器错误。 –