Actual Problem: I named my function 'sort', and it called sort without specifying std, and now I feel dumb.
Solution: Rename my function and explicitly invoke std::sort from now on (like everyone but my professors advise me to do).
I'm trying to figure out std::sort by sorting a vector(cat) of class (Song) pointers based upon strings in that class.
I'm specifically trying to adapt this solution std::sort() on a vector of Class pointers, but without success.
The best I've been able to come up with is:
void Catalog::sort()
{
sort(cat.begin(), cat.end(), compareTitle );
return;
}
bool compareTitle(Song* a, Song* b)
{
return(a->getTitle().compare(b->getTitle()) < 0);
}
Here compareTitle is not a class function. Compiler returns
catalog.cpp: In member function ‘void Catalog::sort()’:
catalog.cpp:62:44: error: no matching function for call to ‘Catalog::sort(std::vector<Song*>::iterator, std::vector<Song*>::iterator, bool (&)(Song*, Song*))’
62 | sort(cat.begin(), cat.end(), compareTitle );
| ^
catalog.cpp:59:6: note: candidate: ‘void Catalog::sort()’
59 | void Catalog::sort()
| ^~~~~~~
catalog.cpp:59:6: note: candidate expects 0 arguments, 3 provided
I have also tried
void Catalog::sort()
{
sort(cat.begin(), cat.end(), [](Song* a, Song* b){
return(a->getTitle().compare(b->getTitle()) < 0);
});
return;
}
Which gives me
catalog.cpp: In member function ‘void Catalog::sort()’:
catalog.cpp:66:7: error: no matching function for call to ‘Catalog::sort(std::vector<Song*>::iterator, std::vector<Song*>::iterator, Catalog::sort()::<lambda(Song*, Song*)>)’
66 | } );
| ^
catalog.cpp:59:6: note: candidate: ‘void Catalog::sort()’
59 | void Catalog::sort()
| ^~~~~~~
catalog.cpp:59:6: note: candidate expects 0 arguments, 3 provided
Any advice is appreciated; my searches have not been fruitful.