2014-09-29 2 views
-1

Рассмотрим ниже код:NULL сейф равно оператор в C++

BST.h

#ifndef BST_H 
#define BST_H 

#include <iostream> 

typedef char Key; 
typedef int Value; 
#define NULL 0 

class BST{ 

private: 
    class Node{ 
     Key key; 
     Value value; 
     Node* left; 
     Node* right; 
     int N; 
    public: 
     Node(Key key='A',Value value=NULL,Node* left=NULL,Node* right=NULL,int N=0): 
     key(key),value(value),left(left),right(right),N(N) 
     { 
     std::cout << "(Node created) Key: " << key << " Value : " << value << std::endl; 
     N++; 
     } 
     int getN() 
     { 
     return N; 
     } 
     bool operator==(Node& node) 
     { 

      if (this->key == node.key && this->value == node.value && this->left == node.left && 
       this->right == node.right && this->N == node.N) 
       return true; 
      else 
       return false; 
     } 
    }; 

Node& Root; 

public: 

    int size(); 
    int size(Node& node); 

}; 


#endif 

И BST.cpp

#include "BST.h" 
#include <iostream> 


int BST::size() 
{ 
return size(Root); 
} 

int BST::size(Node& node) 
{ 
if(node == NULL)//here 
    return 0; 
else 
    return node.getN(); 
} 

Я получаю ошибку компиляции в //here в коде.

bst.cpp(12): error C2679: binary '==' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) 
1>   c:\users\gaurav1.k\documents\visual studio 2010\projects\bst\bst\bst.h(30): could be 'bool BST::Node::operator ==(BST::Node &)' 
1>   while trying to match the argument list '(BST::Node, int)' 

Один из способов устранения этой ошибки является изменить равный оператору как: bool operator==(Node* node)

Как устранить эту ошибку, когда я передаю узел в качестве опорного .i.e. bool operator==(Node& node)

Благодаря

+0

Что вы пытаетесь проверить с помощью 'if (node ​​== NULL)'? Если это значение «по умолчанию», вы можете использовать 'if (node ​​== Node())' вместо этого. – Niall

+1

осторожно с вами constructore. значение не может быть NULL, только левое и правое могут иметь значение NULL, так как они являются единственными указателями. –

ответ

7

node является ссылкой, так что не может быть NULL или 0когда-либо.

Смежные вопросы