需要类型名称错误(与模板相关的错误) [英] Need typename error (template related error)
问题描述
我在另一个有模板的类中有一个名为node的类.Node类的一些方法返回Node指针.这是我如何实现的摘录
I have a class called node inside another class which is templated. Some of the methods of class Node returns Node pointer. This is an excerpt of how I implemented
template <typename T>
class myClass{
....
class Node{
Node* getNodePointer();
...
}
}
...
template <typename T>
myClass<T>::Node* myClass<T>::Node::getNext()
{ return next; }
当我编译以上代码时,出现此错误"myClass.h:138:1:错误:在'myClass :: Node'之前需要'typename',因为'myClass'是从属作用域".我该如何解决这个问题?非常感谢
When I compile above code, I get this error " myClass.h:138:1: error: need ‘typename’ before ‘myClass::Node’ because ‘myClass’ is a dependent scope". How do I fix this problem? Many thanks
推荐答案
如何解决此问题?
How do I fix this problem?
关于这一点,编译器的错误消息非常清楚:
要使用模板类中的嵌套 class
es, struct
s或 typedef
initions,您需要添加 typename
关键字告诉编译器您想使用它作为一种类型:
The compiler's error message s pretty clear about this point:
To use nested class
es, struct
s or typedef
initions from a template class you need to add the typename
keyword to tell the compiler you want to use it as a type:
template <typename T>
typename myClass<T>::Node* myClass<T>::Node::getNext() { // ...
// ^^^^^^^^ << Add typename keyword
这篇关于需要类型名称错误(与模板相关的错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!