C++ "sort" stops working when move inside class

Started by
9 comments, last by Domarius 16 years, 10 months ago
This works - having all the code out in the main.cpp

bool CompareMyObjects(myObject * obj1, myObject * obj2)
{
    //...
}

//...

sort(myObjects.begin(), myObjects.end(), CompareMyObjects);

But when I move the stuff inside a class;


bool myObjectsDB::CompareMyObjectsInClass(myObject * obj1, myObject * obj2)
{	
    //...
}

void myObjectsDB::Sort()
{
	sort(m_vector.begin(), m_vector.end(), CompareMyObjectsInClass);
}



I get the error; d:\Programs\Visual Studio .NET\include\algorithm(1862): error C2064: term does not evaluate to a function taking 2 arguments Can someone tell me the really simple thing that I don't know yet that will make this work?

For local multiplayer retro themed games, visit Domarius Games.

Advertisement
Move CompareMyObjectsInClass to outside of the class. I guess it wouldn't be in the class then, but it doesn't belong there in the first place.
I would think that your main problem is that by itself the method name "CompareMyObjectsInClass" is in the abstract (i.e. since you're working with a class, it doesn't know which instance of the CompareMyObjectsInClass method....)

In myObjectsDB::Sort() try this:
  sort(m_vector.begin(), m_vector.end(), this->CompareMyObjectsInClass);

Another option is to declare CompareMyObjectsInClass static.

"Moving it into the class" makes it into a member function; non-static member functions have an extra hidden argument, the this pointer.

Right now, your CompareMyObjectsInClass function, being a non-static member function, really has THREE arguments (obj1, obj2, and this) and thus will cause an error when you try to use it in the sort template.
Thanks Anthony - that worked! I actually had no idea about the extra hidden "this" parameter... how insidious...

DJHoy - it makes sense now that you put it that way. Sort is not part of an instantiated object...

Vorpy - thanks for the advice, the thing is, I'm looking for the "proper" way to do it too. And since my sorting function is specificaly made to operate on the objects contained in my myObjectsDB class, I figured it should belong there... am I wrong?

For local multiplayer retro themed games, visit Domarius Games.

The this pointer allows access to a class instances members, the compare does not need this access to do its job, this is why some have said it doesnt really belong there, and so, should be either static or global.
I guess I am just thinking - if I want this class to be as self contained as possible, it shouldn't have such a function sitting out in the global area, it should be in its cpp. How would you usually do such a thing?

For local multiplayer retro themed games, visit Domarius Games.

Could Try static as suggested, or lookup functors (function objects) and have an outside type handle compares.
Yeah I finally got my head around this and think static is the way to go for this one. Thanks everyone for your advice.

For local multiplayer retro themed games, visit Domarius Games.

The other option is (if it makes sense in your situation) is to overload operator<(), as all sorting algorithms in the standard library use this as the default.

// This should be at namespace scope.  ie. outside of the class,// but in the same namespace that it's in (if any)bool operator<(const myObject& lhs, const myObject& rhs){    // Do something to compare if lhs < rhs}
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V

This topic is closed to new replies.

Advertisement