开发者

ISO C++ forbids declaration of ‘Node’ with no type

开发者 https://www.devze.com 2023-02-15 21:05 出处:网络
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.

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. 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:

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). Consequently, it doesn't know that Tree<ExpressionOper<T> >::Node. You use the typename keyword to hint to the compiler that you mean a type, and then parsing can succeed. 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.


Maybe you need to use the typename keyword:

inline typename Tree<ExpressionOper< T > >::Node* etc...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号