1

Edit: I've read through the guide at What are the basic rules and idioms for operator overloading? but I just can't figure out what is throwing the error. I am not hurt by down votes, but if you could explain what's causing you to down vote, I'd really appreciate it. I'm new to this community and don't want to ask questions incorrectly or piss people off, but I really do need help. Nobody can figure out why it wasn't working.

I am getting an error: Invalid Operands to Binary Expression. I am sorting a linked list of objects using the below bubbleSort method. It is throwing the error at the "*it1 > *it2. The dereference returns the info stored in the linked list.

In this case, it is 2 objects, Mine. I will paste the class definition with the overloaded comparison operators below. Any help is appreciated, I really can't figure out what is wrong. I have successfully been able to get the comparison operators to work outside of the sort function.

If more information is needed, let me know. Full disclosure, this is a class project. I am not asking for code, just a solution to this error, which my professor was even stumped by.

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";
}

And the Class declaration for Mine

class Mine
{
    friend std::ostream& operator<<(std::ostream &os, const Mine &m);


protected:
    string mineID;
    string mineName;
    string nearestTown;
    string mineStatus;
    Date mineStatusDate;

public:
    ~Mine();
    Mine(string = "0000000", string = "unknown", string = "unknown", string = "Abandoned",
         Date = Date(1900, 1, 1));

    string getMineID() const { return mineID; }
    string getMineName() const { return mineName; }
    string getNearestTown() const { return nearestTown; }
    string getMineStatus() const  { return mineStatus; }
    Date getStatusDate() const { return mineStatusDate; }
    void setMineID(string mID);
    void setMineStatus(string mStat);
    bool operator>(Mine &m);
    bool operator<(Mine &m);
    bool operator==(Mine &m);

    virtual void execReport1(ostream & = std::cout) const;

};

And the overloaded comparisons:

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();
}
wem18
  • 73
  • 1
  • 8
  • Possible duplicate of [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user0042 Aug 09 '17 at 22:20
  • Have you tried making the comparison operators const functions taking a const reference parameter? – scohe001 Aug 09 '17 at 22:30
  • @scohe001 Just tried that. Now I am getting a Mach-o error instead of the operand error. Any clue why? – wem18 Aug 09 '17 at 22:40
  • @wem18 Mach-o linker error? Can you post it? – scohe001 Aug 09 '17 at 22:41
  • @user0042 I read through that, but still couldn't really figure it out. I'm doing my best to not post an unnecessary question but I'm really confused here. – wem18 Aug 09 '17 at 22:42
  • @scohe001 It says "linker command failed with exit code 1 (use -v to see invocation)" in Xcode on a Mac. What can I do to get you more information? Usually the error points out more information, but none here. – wem18 Aug 09 '17 at 22:43
  • @wem18 after some quick googling, looks like you might be accidentally including a `.cpp` somewhere or have double implemented the main function. Without seeing all of your code it's a little hard to guess just off of that error message. – scohe001 Aug 09 '17 at 22:56
  • @scohe001 Yep, that was it. I figured it out about a minute before you commented that, I had included the .h and the .cpp. Stupid mistake on my part. So it was the const part of the overloaded operator that was messing it up, thanks so much for the help! – wem18 Aug 09 '17 at 22:58

1 Answers1

2

You are missing the const qualifiers.

Change the functions to:

bool operator<(Mine const& m) const;
bool operator>(Mine const& m) const;
bool operator==(Mine const& m) const;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thank you so much for the help! Yep, it looks like this was it. Sorry about having such a dumb question, I have like 2000 lines of code in this project so I was just having trouble finding what was wrong. Really appreciate the help. Out of curiosity, any clue why I was able to perform comparison operations on Mine objects outside of the sort method even though I was missing the const declarations? – wem18 Aug 09 '17 at 23:00