[英]ISO C++ forbids declaration of ‘Node’ with no type
i'm making a porting of a project compiled under linux 3 on RHEL 5.0, therefore with a gcc compiler version 4.1.1. 我正在移植RHEL 5.0上在linux 3下编译的项目,因此使用gcc编译器版本4.1.1。 I got this error on a line :
我在一行上遇到了这个错误:
inline Tree<ExpressionOper< T > >::Node* getRootNode() const throw() { return m_rootPtr; }
Follow the tree.h included on top, where is a template declaration of a class: 按照top.h中包含的tree.h进行操作,其中是类的模板声明:
template <typename T>
class Tree
{
public:
class Node
{
public:
Node ()
: _parent (NULL) {};
explicit Node (T t)
: _parent (NULL)
, _data (t) {};
Node (T t, Node* parent)
: _parent (parent)
, _data (t) {};
~Node()
{
for (int i = 0; i < num_children(); i++){
delete ( _children [ i ] );
}
};
inline T& data()
{
return ( _data);
};
inline int num_children() const
{
return ( _children.size() );
};
inline Node* child (int i)
{
return ( _children [ i ] );
};
inline Node* operator[](int i)
{
return ( _children [ i ] );
};
inline Node* parent()
{
return ( _parent);
};
inline void set_parent (Node* parent)
{
_parent = parent;
};
inline bool has_children() const
{
return ( num_children() > 0 );
};
void add_child (Node* child)
{
child -> set_parent ( this );
_children.push_back ( child );
};
private:
typedef std::vector <Node* > Children;
Children _children;
Node* _parent;
T _data;
};
Many thanks in advance. 提前谢谢了。
Try the following, and read this : 试试以下内容,阅读这个 :
inline typename Tree<ExpressionOper< T > >::Node* getRootNode() const throw()
{
return m_rootPtr;
}
In short, since ExpressionOper<T>
is a template type, at the parsing stage the compiler doesn't really know what the contents of Tree<ExpressionOper<T> >
are (until it knows T
). 简而言之,由于
ExpressionOper<T>
是一种模板类型,因此在解析阶段编译器并不真正知道Tree<ExpressionOper<T> >
的内容是什么(直到它知道T
)。 Consequently, it doesn't know that Tree<ExpressionOper<T> >::Node
. 因此,它不知道
Tree<ExpressionOper<T> >::Node
。 You use the typename
keyword to hint to the compiler that you mean a type, and then parsing can succeed. 您使用
typename
关键字来提示您所指的类型的编译器,然后解析可以成功。 The symbol lookup happens later in the compilation process. 符号查找在编译过程的后期发生。
The specific error you got is a quirk of the compiler: since it did not manage to notice that you had a type, it next assumed that you were trying to declare a variable called "Node" in the namespace or class Tree<ExpressionOper< T > >
, and of course if you had been doing that then you would have missed out its type. 你得到的具体错误是编译器的一个怪癖:因为它没有注意到你有一个类型,它接下来假设你试图在命名空间或类
Tree<ExpressionOper< T > >
声明一个名为 “Node”的变量Tree<ExpressionOper< T > >
,当然,如果你一直这样做,那么你会错过它的类型。
也许你需要使用typename关键字:
inline typename Tree<ExpressionOper< T > >::Node* etc...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.