By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,503 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,503 IT Pros & Developers. It's quick & easy.

Problem with stable_sort when used inside a object's method

P: n/a
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
Aug 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Andreas Schmitt wrote:
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
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Aug 14 '05 #2

P: n/a
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.
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.

Aug 14 '05 #3

P: n/a
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 14 '05 #4

P: n/a
> 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...
Aug 14 '05 #5

P: n/a
Andreas Schmitt wrote:
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.
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.


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

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Aug 14 '05 #6

P: n/a
Andreas Schmitt wrote:
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

Aug 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.