Sign in to follow this  
Sean_Seanston

Trouble Sorting a Vector of Objects

Recommended Posts

Sean_Seanston    880
Shouldn't be topic-worthy I know but I've searched around and still can't solve my problem. Maybe I'm doing something small wrong but it seems to look just like other example code from what I can tell but maybe there's something different with the context... I want to sort a vector of objects representing enemy spawns by their y variable. I have a World class with this function:
bool sortEnemies( const EnemySpawnList& e1, const EnemySpawnList& e2 ){ return ( e1.getY() < e2.getY() ); }

Where getY() returns a float. Then I call this in another function of the World class:
std::sort( enemySpawnLists.begin(), enemySpawnLists.end(), sortEnemies );

Where enemySpawnLists is a vector of EnemySpawnList objects. The compiler gives me: error C2662: 'EnemySpawnList::getY' : cannot convert 'this' pointer from 'const EnemySpawnList' to 'EnemySpawnList &' Which I think may be something to do with using those functions that return a value rather than a value itself since I messed around and that seemed to get rid of it. Is that right? That seems weird... hmm... Also I get: error C3867: 'World::sortEnemies': function call missing argument list; use '&World::sortEnemies' to create a pointer to member Now I've tried replacing the sortEnemies function etc. with what looked to me like the same code on cplusplus.com with simple ints and I still got errors. Any ideas? :/

Share this post


Link to post
Share on other sites
tivolo    1367
The second error means that you probably mixed up free functions/member functions. If you want sortEnemies to be a member function, then it must be declared static (otherwise it needs the this-pointer).

Share this post


Link to post
Share on other sites
Aardvajk    13205
Quote:
Original post by Antheus
enemySpawnLists is declared or passed as const. const containers cannot be sorted.


Or getY() is not declared as a const member (which it almost certainly should be):


class Whatever
{
public:
int getY() const { return y; }
};

Share this post


Link to post
Share on other sites
Sean_Seanston    880
Quote:
Original post by Antheus
enemySpawnLists is declared or passed as const. const containers cannot be sorted.

Where does enemySpawnLists come from?


enemySpawnLists definitely isn't declared const and I'm pretty sure it's never passed as const anywhere. It's just the iterators of enemySpawnList that are passed const, that should be ok, right?

enemySpawnLists is a part of the World class.

Quote:
Original post by tivolo
The second error means that you probably mixed up free functions/member functions. If you want sortEnemies to be a member function, then it must be declared static (otherwise it needs the this-pointer).


Hmm... I tried this-> and it still tells me I'm missing the argument list.

Also, it still says:
error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided

Which sounds like it's mistaking the 3 argument sort for the 2 argument sort...

Quote:
Original post by Aardvajk
Or getY() is not declared as a const member (which it almost certainly should be):


class Whatever
{
public:
int getY() const { return y; }
};


Ah, that solved that problem. I was wondering what was wrong since I knew the objects were const but getY() didn't change anything. How does that work? Does it now allow const objects to call functions unless they're declared const and thereby are certain not to change anything? I have to admit I don't remember ever even seeing const in a function declaration after the function name...

Share this post


Link to post
Share on other sites
taz0010    277
Const after the function name means that calling the function doesn't modify the object. If you have a const pointer or reference to an object, you can only call methods which are const.

Share this post


Link to post
Share on other sites
chapter78    146
Yeah, change the function declaration to static and then make sure your sort call looks like this:

std::sort( enemySpawnLists.begin(), enemySpawnLists.end(), ClassName::sortEnemies );


Where ClassName is the actual name of your class :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this