C++ 使用重载运算符的二进制表达式的操作数无效

C++ 使用重载运算符的二进制表达式的操作数无效,c++,operator-overloading,operands,C++,Operator Overloading,Operands,编辑:我已经在阅读了指南,但我就是不知道是什么导致了错误。我没有被否决票伤害,但如果你能解释是什么导致你否决票,我将非常感激。我是这个社区的新手,不想问错问题或惹人生气,但我真的需要帮助。没人能弄明白它为什么不起作用 我得到一个错误:二进制表达式的操作数无效。我正在使用下面的bubbleSort方法对对象的链接列表进行排序。它在“*it1>*it2”处抛出错误。取消引用将返回存储在链接列表中的信息 在这种情况下,它是2个对象,我的。我将用下面重载的比较运算符粘贴类定义。非常感谢您的帮助,我真的无

编辑:我已经在阅读了指南,但我就是不知道是什么导致了错误。我没有被否决票伤害,但如果你能解释是什么导致你否决票,我将非常感激。我是这个社区的新手,不想问错问题或惹人生气,但我真的需要帮助。没人能弄明白它为什么不起作用

我得到一个错误:二进制表达式的操作数无效。我正在使用下面的bubbleSort方法对对象的链接列表进行排序。它在“*it1>*it2”处抛出错误。取消引用将返回存储在链接列表中的信息

在这种情况下,它是2个对象,我的。我将用下面重载的比较运算符粘贴类定义。非常感谢您的帮助,我真的无法找出问题所在。我已经成功地使比较运算符在排序函数之外工作

如果需要更多的信息,让我知道。完全公开,这是一个课堂项目。我不是要代码,只是为了解决这个错误,我的教授甚至被这个错误难倒了

template<typename T>
void LL<T>::bubbleSortI()
{
bool hasChanged = true;
int swapcount = 0;

while (hasChanged)
{
    int changecount = 0;
    LL_iterator<T> it1 = begin();
    LL_iterator<T> it2 = begin();
    ++it2;

    while(it2!=trailer)
    {
        if (*it1 > *it2)
        {
            it1.swap(it2);
            swapcount++;
            changecount++;
        }
        ++it1;
        ++it2;
    }
    if(changecount==0)
    {
        hasChanged = false;
    }
}
cout << swapcount << " swaps performed on the list. The list is now ordered." << "\n";
}
模板
void LL::bubbleSortI()
{
bool hasChanged=true;
int swapcount=0;
虽然(改变了)
{
int changecont=0;
LL_迭代器it1=begin();
LL_迭代器it2=begin();
++it2;
while(it2!=拖车)
{
如果(*it1>*it2)
{
it1.互换(it2);
swapcount++;
changecount++;
}
++it1;
++it2;
}
if(changecont==0)
{
hasChanged=false;
}
}

cout您缺少
const
限定符

将功能更改为:

bool operator<(Mine const& m) const;
bool operator>(Mine const& m) const;
bool operator==(Mine const& m) const;
bool操作符(矿山常量&m)常量;
布尔运算符==(矿山常数&m)常数;

可能的重复项您是否尝试过让比较运算符const函数使用const引用参数?@scohe001刚刚尝试过。现在我得到的是Mach-o错误而不是操作数错误。有什么线索吗?@wem18 Mach-o链接器错误?您能发布吗?@user0042我读了一遍,但仍然无法真正理解。我很抱歉尽我所能不发布不必要的问题,但我在这里真的很困惑。非常感谢你的帮助!是的,看起来就是这样。很抱歉有这么一个愚蠢的问题,我在这个项目中有2000行代码,所以我很难找到错误。非常感谢你的帮助。出于好奇,有什么线索可以解释为什么我能够在sort方法之外对我的对象执行比较操作,尽管我缺少const声明?
bool Mine::operator<(Mine &m)
{
    return getMineID() < m.getMineID();
}

bool Mine::operator>(Mine &m)
{
    return getMineID() > m.getMineID();
}

bool Mine::operator==(Mine &m)
{
    return getMineID() == m.getMineID();
}
bool operator<(Mine const& m) const;
bool operator>(Mine const& m) const;
bool operator==(Mine const& m) const;