Problem with stable_sort when used inside a object's method

A

Andreas Schmitt

Hi,


with myTeam being a vector of pointers to a class 'Team'

std::vector<Team*> myTeam;

I tried to use these two methods in the class 'League'


void League::CalculateTable( void )
{
std::stable_sort( myTeam.begin(), myTeam.end(),
TeamIsBetter );
}

bool League::TeamIsBetter( Team* Team1, Team* Team2 )
{
int PointsTeam1 = Team1->GetPoints();
int PointsTeam2 = Team2->GetPoints();

int TotGoalsTeam1 = Team1->GetTotGoals();
int TotGoalsTeam2 = Team2->GetTotGoals();

int PosGoalsTeam1 = Team1->GetPosGoals();
int PosGoalsTeam2 = Team2->GetPosGoals();

if ( PointsTeam1 != PointsTeam2 )
{
return PointsTeam1 > PointsTeam2;
}
else
{
if ( TotGoalsTeam1 != TotGoalsTeam2 )
{
return TotGoalsTeam1 > TotGoalsTeam2;
}
else
{
return PosGoalsTeam1 > PosGoalsTeam2;
}
}
}

This produces "error C2064: term does not evaluate to a function taking 2
arguments" in <algorithm> at 3 points which contain ' if (_Pred(*_Mid,
*_First)) '

It worked before when I called the stable_sort directly from the main()
function and had the TeamIsBetter() function also defined as a function for
the main() function.
I didn't change anything in the TeamIsBetter() function and I call the
stable_sort() function just the way I did before..
Now that I tried to pack those into a class it doesn't seem to work anymore,
and I don't know that the error wants to tell me in this context...

Anyone with an idea what's the problem here?

Thanks
 
A

Artie Gold

Andreas said:
Hi,


with myTeam being a vector of pointers to a class 'Team'

std::vector<Team*> myTeam;

I tried to use these two methods in the class 'League'


void League::CalculateTable( void )
{
std::stable_sort( myTeam.begin(), myTeam.end(),
TeamIsBetter );
}

bool League::TeamIsBetter( Team* Team1, Team* Team2 )
{
int PointsTeam1 = Team1->GetPoints();
int PointsTeam2 = Team2->GetPoints();

int TotGoalsTeam1 = Team1->GetTotGoals();
int TotGoalsTeam2 = Team2->GetTotGoals();

int PosGoalsTeam1 = Team1->GetPosGoals();
int PosGoalsTeam2 = Team2->GetPosGoals();

if ( PointsTeam1 != PointsTeam2 )
{
return PointsTeam1 > PointsTeam2;
}
else
{
if ( TotGoalsTeam1 != TotGoalsTeam2 )
{
return TotGoalsTeam1 > TotGoalsTeam2;
}
else
{
return PosGoalsTeam1 > PosGoalsTeam2;
}
}
}

This produces "error C2064: term does not evaluate to a function taking 2
arguments" in <algorithm> at 3 points which contain ' if (_Pred(*_Mid,
*_First)) '

It worked before when I called the stable_sort directly from the main()
function and had the TeamIsBetter() function also defined as a function for
the main() function.

I don't know exactly what you think you mean with "defined as a function
for the main() function"...but hold that thought.
I didn't change anything in the TeamIsBetter() function and I call the
stable_sort() function just the way I did before..
Now that I tried to pack those into a class it doesn't seem to work anymore,
and I don't know that the error wants to tell me in this context...

Ah, but you *did* change something significant! Member functions and
`free' functions are vastly different creatures; the former having an
implicit `this' argument, denoting the object upon which it is called.

This has been covered rather endlessly in the past. I would recommend
that you *read the FAQ*; if there's still something you don't
understand, by all means post a further question.

Cheers,
--ag
 
A

Andreas Schmitt

If I had wanted "Go read the FAQ" comments I wouldn't have posted here.
I wanted help here because I didn't find anything in the sources that I have
(on the web and on paper).
I actually don't care if thats some sort of rule on the usenet or something
but if I know an answer to a question
(and you appear to know it) then I usually tell it instead of telling the
guy with the question to first look somewhere
else and then come back to me. If I am not in the mood of answering then I
don't post at all.
Read the FAQ first is one of the rules in usenet so it isn't necessary to
repeat it over and over in every thread.
Anyone who posts a question here either read the FAQ already and didn't find
anything or has never really used
usenet before.
So one could think about simply posting the answer and then maybe an
additional remainder of the FAQ.
But just a "Read the FAQ" post is simply now helpful and a waste of time and
bytes.
 
A

Alf P. Steinbach

* Andreas Schmitt:
If I had wanted "Go read the FAQ" comments I wouldn't have posted here.

This seems to say you posted in an appropriate newsgroup _intentionally_.


[six-year-old's diatribe snipped]
So one could think about simply posting the answer and then maybe an
additional remainder of the FAQ.

That's what Artie did.

You're a moron with a very bad attitude; please disappear.
 
A

Andreas Schmitt

You're a moron with a very bad attitude; please disappear.

Did I at any point insult someone?
Thanks for getting personal

If that's the way netiquette is used in here...
 
A

Artie Gold

Andreas said:
If I had wanted "Go read the FAQ" comments I wouldn't have posted here.
I wanted help here because I didn't find anything in the sources that I have
(on the web and on paper).
I actually don't care if thats some sort of rule on the usenet or something
but if I know an answer to a question
(and you appear to know it) then I usually tell it instead of telling the
guy with the question to first look somewhere
else and then come back to me. If I am not in the mood of answering then I
don't post at all.
Read the FAQ first is one of the rules in usenet so it isn't necessary to
repeat it over and over in every thread.
Anyone who posts a question here either read the FAQ already and didn't find
anything or has never really used
usenet before.
So one could think about simply posting the answer and then maybe an
additional remainder of the FAQ.
But just a "Read the FAQ" post is simply now helpful and a waste of time and
bytes.
Not only have you top-posted, but you snipped out the relevant part of
my original answer, namely:

"Ah, but you *did* change something significant! Member functions and
`free' functions are vastly different creatures; the former having an
implicit `this' argument, denoting the object upon which it is called."

I hope this is sufficiently clear for you.

HTH,
--ag
 
G

Greg

Andreas said:
Hi,


with myTeam being a vector of pointers to a class 'Team'

std::vector<Team*> myTeam;

I tried to use these two methods in the class 'League'


void League::CalculateTable( void )
{
std::stable_sort( myTeam.begin(), myTeam.end(),
TeamIsBetter );
}

bool League::TeamIsBetter( Team* Team1, Team* Team2 )
{
int PointsTeam1 = Team1->GetPoints();
int PointsTeam2 = Team2->GetPoints();

int TotGoalsTeam1 = Team1->GetTotGoals();
int TotGoalsTeam2 = Team2->GetTotGoals();

int PosGoalsTeam1 = Team1->GetPosGoals();
int PosGoalsTeam2 = Team2->GetPosGoals();

if ( PointsTeam1 != PointsTeam2 )
{
return PointsTeam1 > PointsTeam2;
}
else
{
if ( TotGoalsTeam1 != TotGoalsTeam2 )
{
return TotGoalsTeam1 > TotGoalsTeam2;
}
else
{
return PosGoalsTeam1 > PosGoalsTeam2;
}
}
}

This produces "error C2064: term does not evaluate to a function taking 2
arguments" in <algorithm> at 3 points which contain ' if (_Pred(*_Mid,
*_First)) '

Declare League::TeamIsBetter with the "static" keyword in League's
class declaration. Only static routines (and function objects) can be
used as predicates for the routines in <algorithm>.

Stylistically, there is some improvement that could be made to this
code. For example all the local variables (probably added for
debugging) are unnecessary and should be removed. Also, variable names
that differ only by the last character are hard to tell apart and makes
errors more likely. Lastly, since TeamIsBetter returns as soon as it
has an answer, the else clauses are unnecessary.

Putting all these suggestions together:

bool League::TeamIsBetter( const Team* leftTeam, const Team*
rightTeam )
{
if ( leftTeam->GetPoints() != rightTeam->GetPoints())
{
return leftTeam->GetPoints() > rightTeam->GetPoints();
}

if ( leftTeam->GetTotGoals() != rightTeam->GetTotGoals())
{
return leftTeam->GetTotGoals() > rightTeam->GetTotGoals();
}

return leftTeam->GetPosGoals() > rightTeam->GetPosGoals();
}

or in a more compact form:

bool League::TeamIsBetter( const Team* leftTeam, const Team* rightTeam
)
{
return leftTeam->GetPoints() != rightTeam->GetPoints() ?
leftTeam->GetPoints() > rightTeam->GetPoints() :
leftTeam->GetTotGoals() != rightTeam->GetTotGoals() ?
leftTeam->GetTotGoals() > rightTeam->GetTotGoals() :
leftTeam->GetPosGoals() > rightTeam->GetPosGoals();
}

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top