c++ - 无法在模板类中使用 typedef 编译示例

标签 c++ templates typedef

我有这个示例代码: #包括

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/

相关文章:

c++ - 为什么下面的代码可以用 MSVC++ 编译?

C++函数无法使用模板为基类的子类派生模板

基于 C++ 策略的设计 : Inheritance vs composition

c++ - 将 C++ 与 Objective-C 结合使用,如何修复 "Conflicting declaration ' typedef int BOOL'"?

C++ : How to reduce conditional statements

c++ - 使用安卓 NDK 和 C++

c++ - GCC 的 std::sort 与 lambda 的不稳定行为

c++ - 如何使用这个单链表?

c++ - 只允许特定的 `typedef` 作为函数参数

c - 在结构定义中分配下一个指针 = NULL,会引发错误