【发布时间】:2021-07-10 21:18:58
【问题描述】:
谁能解释为什么下面的代码不能编译?至少在 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;
}
【问题讨论】:
-
我编辑了问题以将代码缩进四个空格,而不是使用
。这意味着尖括号不会被解释为 HTML。 -
stackoverflow.com/questions/16284629/…你可以参考这个问题。
-
由于 C++17 不需要额外的定义,参见下面的my answer。