读书人

C++的模版有关问题

发布时间: 2012-09-14 11:53:44 作者: rapoo

求助C++的模版问题
template<class T>
class Test
{
private:
T x;

public:
class Cref;

Test(T r) { x = r; };

Cref te() ;
};

template<class T>
class Test<T>::Cref
{
private:
T t;

public:
Cref(T x)
{
t = x;
}
};

template<class T>
Test<T>::Cref te()
{
return Test<T>::Cref(x);
}

int main(int argc, char *argv[])
{
return 0;
}

这段代码无法编译通过,
在G++下编译后的错误消息提示为:

hello.cpp:错误:‘Test<T>::Cref’之前需要‘typename’,因为‘Test<T>’是一个有依赖的作用域
hello.cpp:错误:expected unqualified-id before ‘int’


[解决办法]
template<class T>
typename Test<T>::Cref te()
提示已经很清楚了
具体自己google 模板参数依赖

读书人网 >C++

热点推荐