Reputation: 449
I am using the std::sort function in <algorithm>
. I followed the examples given at http://www.cplusplus.com/reference/algorithm/sort/. I want to use the example that looks like this:
// using default comparison (operator <):
std::sort (myvector.begin(), myvector.begin()+4);
Applied to my classes, it looks like this:
std::sort(newTuples->begin(), newTuples->end());
where newTuples
is of type
std::vector<Tuple>*
So I overrode the operator<
function of the Tuple
class:
bool const Tuple::operator<(Tuple * tup1){
bool result = false;
for(int iii=0; iii<tup1->size(); iii++){
if(tup1->at(iii)->getTokensValue() < this->at(iii)->getTokensValue()){
result = true;
break;
}
else if (tup1->at(iii)->getTokensValue() > this->at(iii)->getTokensValue())
break;
}
return result;
}
The problem is that when I try to build in XCode, it gives me the error shown:
which code snippet is in <algorithm>
.
It gives me a stack trace of sorts, one line of which gives me the following:
which code snippet is in my Tuple.h
file.
I don't know what else to do! any help is greatly appreciated, as my CS class project is now 3 days late because I can't figure this LAST sorting algorithm out. I already implemented a Relational Database Management System with relations and the relational operations select, project, and rename. I have these tuples correctly formatted, they just need to be in sorted order. Thanks!
EDIT:
So I changed the declarations as suggested in the answers, but now it gives me:
Any help?
Upvotes: 1
Views: 326
Reputation: 63957
This compares a Tuple
against a Tuple*
bool const Tuple::operator<(Tuple * tup1)
You should change it to this:
bool Tuple::operator<( const Tuple &tup1 ) const
Upvotes: 3
Reputation: 76308
You need of an operator that compares const Tuple*
to const Tuple*
. Inside the Tuple class declare:
friend bool operator<(const Tuple*, const Tuple*);
and implement it as a regular function.
Edit
If your vector is std::vector<Tuple>
then you should implement:
bool Tuple::operator<(const Tuple&) const;
instead of your pointer version.
Upvotes: 2