错误:
错误C2678:二进制'==':找不到哪个运算符带有'const entry'类型的左操作数(或者没有可接受的转换)
功能:
template <class T, int maxSize>
int indexList<T, maxSize>::search(const T& target) const
{
for (int i = 0; i < maxSize; i++)
if (elements[i] == target) //ERROR???
return i; // target found at position i
// target not found
return -1;
}
Run Code Online (Sandbox Code Playgroud)
这假设是一个重载运算符吗?作为模板类,我不确定我是否理解错误?
解决方案 - 类中的重载函数现在声明为const:
//Operators
bool entry::operator == (const entry& dE) const <--
{
return (name ==dE.name);
}
Run Code Online (Sandbox Code Playgroud)