I have a self-define class using template, like this:
template<class T>
class foo
{
public:
T a;
bool operator<(const foo<T> &f);
//other functions...
}
template<class T>
bool foo<T>::operator<(const foo<T> &f)
{return a - f.a;}
Now, I new some foos and give them value, then I want to sort this array:
foo<int>* fp = new foo<int>[3];
//give each element value
sort(fp, fp+3); //run-time error
When I use sort function, I got a run-time error.
Did I do something wrong? Please help me.
Probably like this:
std::sortrequires that the comparison function (your less-than operator in this case) defines a strict weak ordering. Your implementation does not, because it is possible for bothA < BandB < Ato be true.