我有一個內部類模板類創建模板內部類對象
template<class param>
class Nested {
param obj;
public:
template<class X>
class Inner {
X obj;
public:
Inner(X obj) : obj(obj) {}
X getObj() { return obj; }
};
Nested();
Nested(param obj) : obj(obj) {}
param getObj() { return obj; }
virtual ~Nested();
};
我嘗試:
Nested<int>::Inner<int> inner(43);
,但我得到的編譯錯誤:
C++/TemplateClass/Debug/../src/TemplateClass.cpp:20: undefined reference to `Nested<int>::~Nested()'
C++/TemplateClass/Debug/../src/TemplateClass.cpp:20: undefined reference to `Nested<int>::~Nested()'
和未來posiblities:
Nested<int>::Inner inner(43);
../src/TemplateClass.cpp: In function ‘int main()’:
../src/TemplateClass.cpp:17:24: error: invalid use of template-name ‘Nested<int>::Inner’ without an argument list
typename Nested<int>::Inner inner(43);
^
../src/TemplateClass.cpp:17:35: error: invalid type in declaration before ‘(’ token
typename Nested<int>::Inner inner(43);
^
../src/TemplateClass.cpp:18:42: error: request for member ‘getObj’ in ‘inner’, which is of non-class type ‘int’
cout << "Inner param object: " << inner.getObj() << endl;
如何創建內部類對象?
可能重複http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the -header-file) – 0x499602D2