我有这个示例代码: #包括
template<class T>
class Class
{
public:
typedef boost::shared_ptr<Class<T> > Ref;
};
template<class T>
class Class2
{
public:
Class<T>::Ref getAReference() {return Class<T>::Ref(new Class<T>);};
};
int main(){}
当我尝试编译它时,我得到:
test.cpp:14: error: type ‘Class<T>’ is not derived from type ‘Class2<T>’
test.cpp:14: error: expected ‘;’ before ‘getAReference’
我不明白,为什么它不起作用?我如何让它发挥作用?
最佳答案
您需要使用 typename
告诉编译器 Ref
是一种类型,即
typename Class<T>::Ref getAReference() {return Class<T>::Ref(new Class<T>);};
这question进一步讨论。
关于c++ - 无法在模板类中使用 typedef 编译示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3476265/