使用模板时出现错误C2678
error C2678 in using template
我正在定义一个优先级队列,并在自定义结构上使用它,但我遇到了这个错误,我不知道如何修复它。
这是我的错误:
error C2678: binary '<' : no operator found which takes a left-hand operand
of type 'const Location' (or there is no acceptable conversion)
我的结构位置
struct Location
{
int x, y, value;
Location(int a, int b);
bool operator == (const Location& other);
bool operator < (const Location& other);
};
Location:: Location(int a, int b) {
x = a;
y = b;
value = 0;
}
bool Location:: operator == (const Location& other) {
return (x == other.x && y == other.y);
}
bool Location:: operator < (const Location& other) {
return value > other.value;
}
这是我的优先队列
template<typename T>
struct my_priority_queue {
priority_queue<T, vector<T>, greater<T>> elements;
bool empty()
{
return elements.empty();
}
void push(T item)
{
elements.emplace(item);
}
T pop()
{
T best = elements.top();
elements.pop();
return best;
}
};
的主要功能
int main() {
Location a(0, 0);
Location b(1, 2);
Location c(3, 0);
my_priority_queue<Location> my_pq;
my_pq.push(a);
}
正如它所说。
操作员不能在LHS上获取const Location
,因为它不是const
函数。
bool operator == (const Location& other) const;
bool operator < (const Location& other) const;
// ^^^^^^
相关文章:
- 错误 C2678:二进制"+":未找到采用类型 'volatile A' 的左侧操作数的运算符(或者没有可接受的转换)
- 在定义C2679和C2678错误代码的地方未找到二进制操作员
- 错误 c2678:二进制'=='
- 错误C2678 C A*路障
- 错误 C2678:二进制'==':找不到采用 'std::pair<const _Kty,_Ty>' 型左操作数的运算符
- (错误 C2678)错误:没有运算符"<<"与这些操作数匹配
- c++找不到带错误c2678的运算符
- 错误 C2678:二进制'>':找不到采用左侧操作数的运算符
- 错误 C2678:二进制">>":找不到采用类型为"std::basic_istream<_Elem,_Traits>"的左操作数的运算符
- 错误 C2678:二进制">>":找不到采用类型为"std::stringstream "的左操作数的运算符
- 错误 C2678:'=='二进制:未找到采用类型 'CSchemaString' 的左操作数的运算符(或者没有可接受的转换)
- 使用模板时出现错误C2678
- 迁移到 VS2013 比较运算符 错误 C2678:二进制"=="无运算符
- 错误 C2678:二进制'==':找不到采用左侧操作数的运算符
- 错误 C2678:二进制"==":找不到采用左操作数类型的运算符(或者没有可接受的转换)
- VS2013中的转换运算符错误C2678,适用于VS2008
- 错误 C2678:二进制'!=':找不到采用左操作数的运算符
- 将C++代码从 VS2003 迁移到 VS2010 后出现错误 C2678,错误 C2679:二进制'=':未找到采用类型为 'int' 的右侧操作数的运算符
- 错误 C2678:二进制'=':未找到采用 'const std::string' 类型左侧操作数的运算符(或没有可接受的转换)
- C++:错误 C2678:二进制"=":未找到采用类型为"const SPoint"的左侧操作数的运算符