c++ - 即使我认为我写了一个,也没有找到运算符
问题描述
VS 2010 一直给我这个错误:
C2678:二进制“==”:未找到采用“const AnyColor”类型的左侧操作数的运算符(或没有可接受的转换)
对于这个表达式:
bool AnyColor::operator ==(const AnyColor &)
在尝试匹配参数列表时(const AnyColor, const AnyColor)
这是整个程序:
#include "stdafx.h"
#include <string.h>
#include <stdint.h>
#include <unordered_map>
#include <cstdlib>
#include <iostream>
// ________________________________________________________________________________
typedef uint16_t cSpace;
// Identifiers for different color spaces
enum {
kCsRGB = 0,
kCsCMYK,
kCsGray
};
// The color class can represent colors from a variety of spaces.
// Up to a 4 dimensional color can be represented.
class AnyColor {
public:
AnyColor(void) {
space = kCsRGB; // Might change; this is an initial guess to avoid meaningless values
memset(axis, 0, sizeof(axis));
};
AnyColor(short type, double a, double b, double c, double d)
{
space = type;
axis[0] = a;
axis[1] = b;
axis[2] = c;
axis[3] = d;
}
void Set(short type, double a, double b, double c, double d)
{
space = type;
axis[0] = a;
axis[1] = b;
axis[2] = c;
axis[3] = d;
}
inline bool operator==(const AnyColor& op) // Equal only if all its members are equal
{
if (space != op.space)
{
return false;
}
for(int i = 0; 4 > i; i++)
{
if (axis[i] != op.axis[i])
{
return false;
}
}
return true;
}
inline bool operator!=(AnyColor &op)
{
return (*this == op) ? false : true;
}
cSpace space;
double axis[4];
};
static std::unordered_map<const AnyColor,int> colormap;
static int seq = -1;
const int numColors = 25;
static double quarters()
{
return 0.25 * static_cast<double>(rand() % 5);
}
int _tmain(int argc, _TCHAR* argv[])
{
AnyColor c;
for(int i = 0; numColors > i; ++i)
{
c.axis[0] = quarters();
c.axis[1] = quarters();
c.axis[2] = quarters();
if (colormap.count(c) == 0) // THIS IS WHERE THE PROBLEM SHOWS UP
{
colormap[c] = ++seq;
}
}
return 0;
}
我已经尝试了通过值传递参数与通过引用传递参数的所有组合,而const
不是 const。我每次都得到同样的错误。我敢打赌我忽略了一些简单的事情。
这个程序什么都不做;当我遇到这个错误时,我才开始写它。我希望最终得到将序列号分配给唯一颜色的代码。
我必须能够使用 VS2010 进行编译,所以我的 C++ 方言仅限于 C++11。
在这个小测试部分中,我生成随机颜色只是为了测试我对unordered_map
. 我需要做什么才能编译它?
解决方案
推荐阅读
- python - 套接字主机名,无法获取 IP
- ios - 尝试在 iOS 设备中颤振运行时,获取 s6Runner11AppDelegateC11application
- java - 空对象引用上的 View.findViewById(int)' 存在问题
- import - Acumatica 导入方案 - 导入 AR 发票 - 失衡错误
- python - 查阅 Python 类的源代码的最佳方法是什么?
- swift - 多行注释的快速词法结构
- excel - 范围内所有最大值的VBA随机行
- html - 如何获得使用悬停在触摸屏上工作的下拉菜单?
- vba - 如果条件在VBA中读取数组的多个项目
- python-3.x - 使用 matplotlib 的颤动图中的错误向量(电压梯度)