Mau*_*Mau 4 c++ templates visual-studio-2010 visual-c++
是否有正确的方法为模板类定义成员函数,它返回子类的实例?
这是一个不能在VC++ 2010中编译的示例:
template<class T> class A {
public:
class B {
public:
T i;
};
A();
B* foo();
};
/////////////////////////////////////////////
template<class T> A<T>::A() {}
template<class T> A<T>::B* A<T>::foo() {
cout << "foo" << endl;
return new B();
}
Run Code Online (Sandbox Code Playgroud)
我明白了
Error 8 error C1903: unable to recover from previous error(s); stopping compilation
Run Code Online (Sandbox Code Playgroud)
在该行,其中定义的foo开始.
我有正确的包含和命名空间声明iostream等.
多谢你们!
编辑:
根据要求,这里是完整的错误列表,所有错误都在同一行:
Warning 1 warning C4346: 'A<T>::B' : dependent name is not a type
Error 2 error C2143: syntax error : missing ';' before '*'
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 4 error C1903: unable to recover from previous error(s); stopping compilation
Warning 5 warning C4346: 'A<T>::B' : dependent name is not a type
Error 6 error C2143: syntax error : missing ';' before '*'
Error 7 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 8 error C1903: unable to recover from previous error(s); stopping compilation
Run Code Online (Sandbox Code Playgroud)
名称A<T>::B是依赖的,您需要提示缩减名称的编译器是类型
template<class T> typename A<T>::B* A<T>::foo() {...}
同样的这一行:return new B();- >return new typename A<T>::B();
阅读:我必须在何处以及为什么要使用"模板"和"typename"关键字?