NULL安全等于C++中的运算符
NULL safe equal to operator in 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:usersgaurav1.kdocumentsvisual studio 2010projectsbstbstbst.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)
当我将节点作为引用传递时,如何解决此错误。即。bool operator==(Node& node)
感谢
node
是一个引用,因此它不能为NULL
或0
。
相关文章:
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 布尔比较运算符是如何在C++中工作的
- 重载运算符new[]的行为取决于析构函数
- 如何防止clang格式在流运算符调用之间添加换行符<<