Problem With Basic Vector Sort

Discussion in 'C++' started by A_StClaire_@hotmail.com, Nov 12, 2005.

  1. Guest

    hi,

    not sure what I'm doing wrong here. getting "error C2064: term does
    not evaluate to a function taking 2 arguments" in response to my
    SortCardVector function...?

    Card.h:

    #pragma once

    #include <vector>

    using namespace std;

    class Card
    {
    int value;
    int suit;

    public:
    Card(void);
    ~Card(void);

    void SetValue(int);

    int GetValue();

    void SetSuit(int);

    int GetSuit();

    void SortCardVector(vector<Card>);
    };

    --------------------

    Card.cpp

    #include "StdAfx.h"
    #include ".\card.h"

    #include <algorithm>

    #using <mscorlib.dll>

    Card::Card(void)
    {
    }

    Card::~Card(void)
    {
    }

    void Card::SetValue(int newValue)
    {
    value = newValue;
    }

    int Card::GetValue()
    {
    return value;
    }

    void Card::SetSuit(int newValue)
    {
    suit = newValue;
    }

    int Card::GetSuit()
    {
    return suit;
    }

    void Card::SortCardVector(vector<Card> targetVector)
    {
    sort(targetVector.begin(), targetVector.end(), value);
    }
     
    , Nov 12, 2005
    #1
    1. Advertising

  2. Neelesh Guest

    wrote:
    > hi,
    >
    > not sure what I'm doing wrong here. getting "error C2064: term does
    > not evaluate to a function taking 2 arguments" in response to my
    > SortCardVector function...?
    >
    > Card.h:
    >
    > void Card::SortCardVector(vector<Card> targetVector)
    > {
    > sort(targetVector.begin(), targetVector.end(), value);
    > }


    1) The usage of std::sort is incorrect. The third argument, if present,
    must be a strict weak ordering function object (not an integer). You
    need to define a separate function object and pass it as the third
    argument.

    2) The types that are passed to containers like vector should be
    copyable, assignable and comparable. Hence, if you donot want a third
    argument, then you need to define operator< for cards.
     
    Neelesh, Nov 12, 2005
    #2
    1. Advertising

  3. Neelesh wrote:
    > wrote:
    >
    >>hi,
    >>
    >>not sure what I'm doing wrong here. getting "error C2064: term does
    >>not evaluate to a function taking 2 arguments" in response to my
    >>SortCardVector function...?
    >>
    >>Card.h:
    >>
    >>void Card::SortCardVector(vector<Card> targetVector)
    >>{
    >> sort(targetVector.begin(), targetVector.end(), value);
    >>}

    >
    >
    > 1) The usage of std::sort is incorrect. The third argument, if present,
    > must be a strict weak ordering function object (not an integer). You
    > need to define a separate function object and pass it as the third
    > argument.
    >
    > 2) The types that are passed to containers like vector should be
    > copyable, assignable and comparable. Hence, if you donot want a third
    > argument, then you need to define operator< for cards.
    >


    As well as the two problems above. You also have these problems.

    targetVector is beign poassed by value, so that even if you do get it
    sorted you are only sorting the copy that is local to SortCardVector.
    You need to use a reference.

    You need to ask yourself why is a function that sorts a vector of Cards
    a member of Card? There is absolutely no logic behind this. It's a
    typical newbie error to think that every piece of code you write must be
    part of some class or other. Instead this function will work perfectly
    well as a function that is not a member of any class.

    To sum up my and Neelesh's post you need to do this

    class Card
    {
    ...
    };

    // opeator < needed so sort works
    bool operator<(Card lhs, Card rhs)
    {
    // think about this, without this function you were
    // expecting C++ to sort your Cards, without telling it
    // when one card is less than another.
    // return true, if lhs is less than rhs, false otherwise
    ...
    }

    // SortCardVector is a free function, not a member of Card
    // and its argument is poassed by reference
    void SortCardVector(vector<Card>& targetVector)
    {
    sort(targetVector.begin(), targetVector.end());
    }

    john
     
    John Harrison, Nov 12, 2005
    #3
  4. Howard Guest

    <> wrote in message
    news:...
    > hi,
    >
    > not sure what I'm doing wrong here. getting "error C2064: term does
    > not evaluate to a function taking 2 arguments" in response to my
    > SortCardVector function...?
    >
    > Card.h:
    >
    > #pragma once
    >
    > #include <vector>
    >
    > using namespace std;
    >


    This isn't the problem you're seeing, but you're also doing something here
    you shouldn't:

    > using namespace std;


    You should probably never put "using namespace whatever;" in a header file,
    since it will then bring in that entire namespace to every file which
    includes this header.

    In headers, it's probably better to just use std:: in front of anything
    you're using from the std namespace (i.e., std::vector). Or else you can
    use "using std::vector" in the header. Either one is better than bringing in
    the entire std namespace, especially since that's a _big_ one!

    -Howard
     
    Howard, Nov 12, 2005
    #4
    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. JerryJ
    Replies:
    11
    Views:
    1,406
    Dave Moore
    Apr 28, 2004
  2. John Black
    Replies:
    6
    Views:
    2,063
    John Harrison
    May 28, 2004
  3. pmatos
    Replies:
    6
    Views:
    23,802
  4. Replies:
    8
    Views:
    1,925
    Csaba
    Feb 18, 2006
  5. Navin
    Replies:
    1
    Views:
    699
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page