谁能解释为什么以下代码无法编译?至少在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) 对不起,如果这个问题对很多人来说似乎微不足道.
在C++代码中,有如下内容:
class Foo
{
public:
static int bands;
...
...
private:
...
...
}//class definition ends
int Foo::bands; //Note: here its not initialized to any value!
Run Code Online (Sandbox Code Playgroud)
当'bands'一次在类中声明为static时,为什么上述语句需要再次使用?
静态变量也可以在任何类中声明为私有成员变量吗?