二进制==的问题
Problem with binary ==
我遇到了二进制operator==
的奇怪问题。
我有一个函数返回:Type< Colors >* get();
、T
为enum Colors {Red,Black}
型我有一个operator==
定义为:
bool operator==(Type<Colors>* left, Colors right)
{
//...
}
现在,在代码中我有行:
if (get() == Red)
{
//
}
但是在这里我得到错误说:
error C2679: binary '==' : no operator found which takes a right-hand operand of type 'Colors' (or there is no acceptable conversion)
1> could be 'built-in C++ operator==(Node<Key_T,Value_T> *, Node<Key_T,Value_T> *)'
1> with
1> [
1> Key_T=int,
1> Value_T=int
1> ]
or 'bool operator ==(const Type<T> *,const Colors)'
1> with
1> [
1> T=Colors
1> ]
1> while trying to match the argument list '(Node<Key_T,Value_T> *, Colors)'
1> with
1> [
1> Key_T=int,
1> Value_T=int
1> ]
显然第二个匹配是我打算使用的,它是完美匹配的,但它不想;)编译。我做错了什么?
(这比答案本身更具有诊断性…但是评论太多了
适用于GGC 4.5.2:
enum Colour { Red, Black };
template <typename T>
struct Type { };
bool operator==(Type<Colour>*, Colour) { return true; }
int main()
{
Type<Colour>* p;
return p == Black;
}
请在您的编译器上尝试上面的错误信息,如果有,请发布错误信息。如果没有,请发布你的确切完整的程序,因为错误可能是一些微妙的东西,你没有发布。
功能operator==
既不修改左也不修改右?
输入const,就可以了
相关文章:
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 二进制搜索树叶数问题
- 二进制表达式 cout 问题的无效操作数
- 有关在C++中解析二进制文件的问题
- openCV c++中手动侵蚀图像的问题(二进制图像)
- 创建二进制文件时出现问题
- 十进制到二进制的实现不能完全适用于我大学的检查器。问题或提示可能是什么
- 在 64 位 Linux armv8 计算机上编译 32 位二进制文件时遇到问题
- 读取二进制文件时出现问题
- 我通过迭代加法将二进制数转换为十进制并检查单个字符(请参阅代码)的方法有什么问题?
- MPI 二进制文件 I/O 基本功能和性能问题
- 快速读取带有数字 c++ 的二进制文件时出现问题
- 通过C 读取二进制文件的问题
- 读取C++中的二进制文件时出现问题
- C++数组长度的二进制搜索问题
- 在SD卡上读取和存储二进制GPS数据方面的问题
- AVL二进制树旋转和删除树的C 问题
- 在 C++ 中使用按位运算符理解十进制到二进制函数的问题
- Wxwidgets 在另一台计算机上运行二进制文件时出现问题
- 非常基本的C++程序问题 - 二进制表达式的操作数无效