试图为模板类

时间:2017-11-04 17:40:54

标签: c++ templates overloading operator-keyword

我看过类似的问题,但没有人问过类似的问题,似乎遇到了和我一样的问题。我正在尝试重载运算符<<对于二叉树节点。这是我的代码:

template <class T>
class BinaryNode
{
public:

    //Constructors
    BinaryNode() : pLeft(NULL), pRight(NULL), pParent(NULL) {}
    BinaryNode(T contents) : data(contents), pLeft(NULL), pRight(NULL), pParent(NULL) {}

    // return size (i.e. number of nodes in tree)
    int size() const
    {
            return 1 +
               (pLeft== NULL? 0 : pLeft->size()) +
               (pRight == NULL ? 0 : pRight->size());
    }

    // add a node the left/right
    void addLeft (BinaryNode <T> * pNode);
    void addRight(BinaryNode <T> * pNode);

    // create a node and add it to the left/right
    void addLeft (const T & t) throw (const char *);
    void addRight(const T & t) throw (const char *);

    // since no validation is done, everything is public
    BinaryNode <T> * pLeft;
    BinaryNode <T> * pRight;
    BinaryNode <T> * pParent;

    // the data of unknown type: cannot validate so is public
    T data;
};

重载的运算符,稍后在同一个文件中:

编辑:修正了我错误地引用成员变量的问题。

template<class T>
ostream& operator<<(ostream& out, const BinaryNode<T>*& rhs)
{
    if (!rhs)
        return out;
    if (rhs->pLeft)
        out << rhs->pLeft;
    out << rhs->data << ' ';
    if (rhs->pRight)
        out << rhs->pRight;
    return out;
}

我收到了大量的错误,如果我注释掉重载的运算符,这些错误都会消失。我在这里做错了什么?

编辑: 如果您想通过它们进行筛选,错误是:

Error   C2143   syntax error: missing ';' before '&'    
Error   C2988   unrecognizable template declaration/definition  
Error   C2146   syntax error: missing ')' before identifier 'filename'  
Error   C2059   syntax error: 'const'   
Error   C2050   switch expression not integral  
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)    
Error   C2065   'word': undeclared identifier   
Error   C2065   'word': undeclared identifier   
Error   C2065   'word': undeclared identifier   
Error   C2065   'word': undeclared identifier   
Error   C2065   'word': undeclared identifier   
Error   C2065   'word': undeclared identifier   
Error   C2065   'word': undeclared identifier   
Error   C2065   'tree': undeclared identifier   
Error   C3861   'testSimple': identifier not found  
Error   C3861   'testMerge': identifier not found   
Error   C3861   'testDisplay': identifier not found 
Error   C3861   'testAdd': identifier not found 
Error   C2065   'T': undeclared identifier  
Error   C2065   'string': undeclared identifier 
Error   C2065   'out': undeclared identifier    
Error   C2065   'ostream': undeclared identifier    
Error   C3861   'huffman': identifier not found
Error   C2065   'fileName': undeclared identifier   
Error   C2065   'fileName': undeclared identifier
Error   C2065   'fileName': undeclared identifier   
Error   C2065   'fileName': undeclared identifier   
Error   C2065   'choice': undeclared identifier 
Error   C2065   'choice': undeclared identifier 
Error   C2923   'BinaryNode': 'T' is not a valid template type argument for parameter 'T'   

1 个答案:

答案 0 :(得分:0)

您的操作员签名是错误的,它应该引用对象,而不是对指向对象的引用:

ostream& operator<<(ostream& out, const BinaryNode<T> & rhs)

您应该相应地更新操作员主体。