相关疑难解决方法(0)

运算符==的C++模板类错误

错误:
错误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)

indexList.h
indexList.cpp

这假设是一个重载运算符吗?作为模板类,我不确定我是否理解错误?

解决方案 - 类中的重载函数现在声明为const:

//Operators
bool entry::operator == (const entry& dE)  const <--
{
    return (name ==dE.name);

}
Run Code Online (Sandbox Code Playgroud)

c++ operators equals-operator

5
推荐指数
2
解决办法
5463
查看次数

标签 统计

c++ ×1

equals-operator ×1

operators ×1