c++ - 模板内模板的typedef

标签 c++ class templates typedef smart-pointers

<分区>

Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?

我有一个类,它在创建对象并将所需类作为模板参数传递时创建智能指针。我有另一个类需要在另一个类中使用该智能指针。

#include <iostream>
using namespace std;

//智能指针类

template<typename T>
class IntrusivePtr
{
public:
    IntrusivePtr()
    {
        cout << "IntrusivePtr()";
    }
};

//我需要一个也是模板的智能指针的类

template<typename T>
class A
{
public:
    A()
    {
        cout << "A()";
    }
    typedef IntrusivePtr< A<T> > my_ptr;
};

//使用智能指针的类。

template<typename T>
class B
{
public:
    B()
    {
        cout << "B()";
    }

    typedef A<T>::my_ptr x;
};



int main()
{
    B<int> ob;

    return 0;
}

这可以用c++实现吗? 我知道新的 C++11 支持 typedef 这样的事情,但我使用的是旧标准:( 编译这个我遇到了一些糟糕的错误:

C:\Users\jacob\typedef_template_class-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2008__Qt_SDK__Debug..\typedef_template_class\main.cpp:41: error: C2146: syntax error : missing ';' before identifier 'x'

C:\Users\jacob\typedef_template_class-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2008__Qt_SDK__Debug..\typedef_template_class\main.cpp:41: error: C2146: syntax error : missing ';' before identifier 'x'

C:\Users\jacob\typedef_template_class-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2008__Qt_SDK__Debug..\typedef_template_class\main.cpp:41: error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int

编辑: 抱歉,我更改了一些内容和错误代码。这就是我想要的样子。抱歉

最佳答案

template<typename T>
class B
{
public:
    B()
    {
        cout << "B()";
    }

    typedef typename A< B >::my_ptr x;
};

您应该使用 typename,因为 my_prtdependent name

关于c++ - 模板内模板的typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12302465/

相关文章:

c++ - v8::Persistent MarkIndependent,这个方法到底是做什么的?

c++ - C++ 中 string 和 int 的函数模板

c++ - 我的自定义 Vector 类的 operator+ 有什么问题?

c++ - 插入通用容器不起作用

模板类中的 C++ 静态常量数组初始化

c++ - 警告 : uninitialized variable//But I have initialized ! C++ 编译器错误?

c++ - 为什么要避免 C++ 中的输入运算符(operator>>)?

c++ - 没有要调用的匹配函数(构造函数),候选函数的不同之处在于它是对给定类型的引用

java - 奇怪的Java代码: Class in a Class

c++ - 如何将 "normal"C++ 类转换为模板?