<分区>
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
编辑: 抱歉,我更改了一些内容和错误代码。这就是我想要的样子。抱歉