Problem with stable_sort when used inside a object's method

Discussion in 'C++' started by Andreas Schmitt, Aug 14, 2005.

  1. 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
     
    Andreas Schmitt, Aug 14, 2005
    #1
    1. Advertising

  2. Andreas Schmitt

    Artie Gold Guest

    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!"
     
    Artie Gold, Aug 14, 2005
    #2
    1. Advertising

  3. 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.
     
    Andreas Schmitt, Aug 14, 2005
    #3
  4. * 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?
     
    Alf P. Steinbach, Aug 14, 2005
    #4
  5. > 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...
     
    Andreas Schmitt, Aug 14, 2005
    #5
  6. Andreas Schmitt

    Artie Gold Guest

    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!"
     
    Artie Gold, Aug 14, 2005
    #6
  7. Andreas Schmitt

    Greg Guest

    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
     
    Greg, Aug 15, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Payne

    std::stable_sort with predicate woes

    William Payne, Apr 21, 2004, in forum: C++
    Replies:
    8
    Views:
    1,625
    tom_usenet
    Apr 21, 2004
  2. bill
    Replies:
    2
    Views:
    428
    P.J. Plauger
    May 25, 2004
  3. John Black
    Replies:
    6
    Views:
    2,081
    John Harrison
    May 28, 2004
  4. Andreas Schmitt

    stable_sort with arrays?

    Andreas Schmitt, Aug 7, 2005, in forum: C++
    Replies:
    1
    Views:
    371
    Kai-Uwe Bux
    Aug 7, 2005
  5. sandy
    Replies:
    12
    Views:
    645
    Clark Cox
    May 30, 2007
Loading...

Share This Page