谁能解释为什么以下代码无法编译?至少在g ++ 4.2.4上.
更有趣的是,为什么它会在我将MEMBER转换为int时进行编译?
#include <vector>
class Foo {
public:
static const int MEMBER = 1;
};
int main(){
vector<int> v;
v.push_back( Foo::MEMBER ); // undefined reference to `Foo::MEMBER'
v.push_back( (int) Foo::MEMBER ); // OK
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用交叉编译器.我的代码是:
class WindowsTimer{
public:
WindowsTimer(){
_frequency.QuadPart = 0ull;
}
private:
static LARGE_INTEGER _frequency;
};
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
对WindowsTimer :: _ frequency'的未定义引用
我也尝试将其改为
LARGE_INTEGER _frequency.QuadPart = 0ull;
Run Code Online (Sandbox Code Playgroud)
要么
static LARGE_INTEGER _frequency.QuadPart = 0ull;
Run Code Online (Sandbox Code Playgroud)
但我仍然得到错误.
有谁知道为什么?