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();
}