简体   繁体   中英

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

也许你需要使用typename关键字:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM