Quote:Original post by daniel_i_l
I read the link and did this:
*** Source Snippet Removed ***
Quote:Original post by JY
Your units vector is declared as std::vector<unit> not vector<unit *> so your sort predicate will expect the parameters to be unit, not unit*.
//BAD!!! v vbool operator()( unit * & elem1, unit * & elem2 )//BAD!!! ^ ^//GOOD!!! v vbool operator()( unit & elem1, unit & elem2 )//GOOD!!! ^ ^//EXTRA CREDIT! vvvvv vvvvv vvvvvbool operator()( const unit & elem1, const unit & elem2 ) const//EXTRA CREDIT! ^^^^^ ^^^^^ ^^^^^
/* Rationale: This is an issue of const correctness. consts #1 and #2 mean we
* won't modify our arguments, and thus it's OK to pass our function const
* objects. Non-const objects are also fine. Note that by doing this,
* assignments such as "elem1.next_step = ..." will generate compiler errors,
* since elem1 is const. This is to prevent a mistake - a good thing in this
* case! Const #3 tells the compiler that the function itself isn't modifying
* anything of it's owning class - thus this becomes legal:
*
* const SortDis sorter;
* bool unit1_before_unit2 = sorter( unit1 , unit2 );
*
* As is, the above code will fail, because operator() is not const, and thus
* cannot be called for a const SortDis (as sorter is).
*/
//BAD!! vv abs(elem1->blah)+ abs(elem1->blah) < abs(elem2->blah)+ abs(elem2->blah) ; //BAD!!! ^^//GOOD!! v abs(elem1.blah)+ abs(elem1.blah) < abs(elem2.blah)+ abs(elem2.blah) ; //GOOD!!! ^
[Edited by - MaulingMonkey on January 27, 2006 1:58:24 AM]