2017-11-04 3 views
-1

비슷한 질문을 한 사람은 없지만 비슷한 질문을하는 사람은 아무도 없습니다. 이진 트리 노드에 대해 연산자 < <을 오버로드하려고합니다. 나중에 동일한 파일에,템플릿 클래스에 삽입 연산자를 과부하하려고 시도합니다.

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' 
+0

"거대한 오류가 발생했습니다." – DimChtz

+0

'operator <<'함수에서'rhs'의 * type *은 무엇입니까? * 포인터가 아닌가? 구조체 *에 대한 포인터의 멤버에 어떻게 액세스합니까? –

+0

'using namespace std'를 사용하고 있습니까? 왜냐하면 오버로드에서'std ::'를 사용해야하기 때문입니다 :'std :: ostream & operator << (std :: ostream & out .....'이것은 나를 위해 잘 컴파일됩니다 –

답변

0

귀하의 운영자 서명이 잘못, 그것은 반대하는 포인터, 참조를하지 개체에 대한 참조를해야 :

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

그리고 운영자 본문을 적절하게 업데이트해야합니다.

관련 문제