我有这段代码可以从 Windows 移植到 Linux。
template<class T, int Size>
class CVector {
/* ... */
};
template<int n, int m>
class CTestClass {
public:
enum { Size = 1 << n };
private:
static CVector<int, Size> a; // main.cpp:19
};
template<int n, int m>
CVector<int, CTestClass<n, m>::Size> CTestClass<n, m>::a; // main.cpp:24
它使用 VS2008 编译,但不使用 g++ 4.3.2。 我收到的错误是:
main.cpp:24: error: conflicting declaration ‘CVector CTestClass::alpha_to’
main.cpp:19: error: ‘CTestClass< n, m >::alpha_to’ has a previous declaration as ‘CVector< int, CTestClass< n, m >::Size > CTestClass< n, m >::alpha_to’
main.cpp:24: error: declaration of ‘CVector< int, CTestClass< n, m >::Size > CTestClass< n, m >::alpha_to’ outside of class is not definition
有人知道如何通过 g++ 使其可编译吗?
谢谢!
最佳答案
这适用于 gcc 3.4 和 4.3 以及 VC8:
template<class T, int Size>
class CVector {
/* ... */
};
template<int n, int m>
class CTestClass {
public:
enum { Size = 1 << n };
typedef CVector<int, Size> Vector;
private:
static Vector a;
};
template<int n, int m>
typename CTestClass<n,m>::Vector CTestClass<n,m>::a;
关于c++ - 模板类的静态模板字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1980642/