2012-07-10 90 views
3

是否有一种为模板类定义成员函数的正确方法,该模板类返回子类的实例?返回子类型的成员模板函数

下面是不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(); 
} 

我得到

Error 8 error C1903: unable to recover from previous error(s); stopping compilation 

在该行的定义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 
+0

给我们 “先前的错误(S)” 了。也许在A :: B缺失之前的typename? – PiotrNycz 2012-07-10 13:50:34

+0

谢谢@PiotrNycz,请参阅编辑。 – Mau 2012-07-10 14:03:21

+0

可能的重复[哪里和为什么我必须把“模板”和“typename”关键字?](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to -put-the-template-and-typename-keywords) – 2012-07-10 15:05:27

回答

相关问题