在类定义中定义静态const整数成员
我的理解是C ++允许在类中定义静态const成员,只要它是整数类型即可。
那么,为什么以下代码会给我一个链接器错误?
#include <algorithm>#include <iostream>class test{public: static const int N = 10;};int main(){ std::cout << test::N << "\n"; std::min(9, test::N);}
我得到的错误是:
test.cpp:(.text+0x130): undefined reference to `test::N'collect2: ld returned 1 exit status
有趣的是,如果我注释掉对std :: min的调用,代码编译和链接就好了(即使test :: N也在前一行引用)。
知道发生了什么事吗?
我的编译器是Linux上的gcc 4.4。
动漫人物
人到中年有点甜
相关分类