Your vector contains not Highscore objects, but pointers to them. You should provide custom comparsion function to std::sort (as you cannot overload less operator for pointers)
What do you mean non members?
Something like that:
1 2 3 4 5 6 7 8 9 10 11
#include <tuple>
//...
booloperator<(HighScore& lhs, HighScore& rhs) //Should be const refs but you need to make your class const correct first
{
return std::make_tuple(lhs.getScore(), lhs.getName()) <
std::make_tuple(rhs.getScore(), rhs.getName());
}
//passin custom comparsion function making use of overloaded operator
std::sort(compareVec.begin(), compareVec.end(), [](HighScore* lhs, HighScore* rhs)
{return *lhs < *rhs;});
I'm sry this is a little advanced for me, is there a way I can fix it with my current code? or more or less close to what I have written? I need to understand the baseline basics before using more advanced alternatives.
p_compare should be standalone function, do not make it member.
I just noticed that you have a vector of Comparable pointers. You should either have vector of HighScore pointers of have comparsion operator (and comparsion function) defined for Comparable objects.
So you want me to take the comparable * vector and make it just Highscore? what If I had multiple classes and wanted 1 vector of pointers to combine them all, wouldn't the way to that be comparable * vector?
Also I made compare a non member and this is the error I got, I am assuming its related to comparable* vs Highscore * of vectors:
error C2664: 'bool (HighScore *,HighScore *)' : cannot convert parameter 2 from 'Comparable *' to 'HighScore *'
Btw I appreciate the time you are taking to help me out.
what If I had multiple classes and wanted 1 vector of pointers to combine them all,
By storing them in vector of comparable pointers, you lose type information and now have to use whichever Comparable has to offer (member functions and data). That means, you cannot use any member introduced in HighScore.
Error you see isrelated to that: compiler cannot downcast safely.