T.T*_*.T. 5 c++ operators equals-operator
错误:
错误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)
首先完全按原样阅读错误文本:
binary'==':找不到带有'const entry'类型左手操作数的运算符
这意味着它找不到任何==接受entry类型作为其左操作数的运算符.此代码无效:
entry const e;
if (e == foo)
Run Code Online (Sandbox Code Playgroud)
您已向我们展示了列表类的代码,但这不是错误的内容.错误是关于该entry类型的运算符缺乏,无论是什么.要么给类一个operator==函数,要么声明一个独立的operator==函数,它接受一个const entry&作为它的第一个参数.
struct entry {
bool operator==(const entry& other) const;
};
// or
bool operator==(const entry& lhs, const entry& rhs);
Run Code Online (Sandbox Code Playgroud)
我认为后者是首选的风格.
问题是指在此实例中使用的类型T没有定义运算符==.我会从你的问题中猜出这是一个班级"入口".
也可能是'entry'类没有正确定义operator ==来获取const条目并作为参数.
| 归档时间: |
|
| 查看次数: |
5463 次 |
| 最近记录: |