此代码在 MSVC 中运行良好,但根据 gcc-4.7.2 C++11,链接器会出现以下问题。它出什么问题了
错误:
/home/r7Qecv/ccEZjv1w.o: In function `main':
prog.cpp:(.text.startup+0xa): undefined reference to `Foo<long>::s'
prog.cpp:(.text.startup+0x17): undefined reference to `Foo<int>::s'
prog.cpp:(.text.startup+0x2c): undefined reference to `Foo<long>::s'
collect2: error: ld returned 1 exit status
代码
#include <iostream>
#include <stack>
using namespace std;
template<class T>
class Foo{
public:
T a;
static T s;
};
template<>
int Foo<int>::s;
template<>
long Foo<long>::s;
int main(){
Foo<int> f;
Foo<long> f2;
f.a=4;
f.s=6;
f2.a=8;
std::cout<<f2.s;
f2.s=11;
return 0;
}