Problem With Basic Vector Sort

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • A_StClaire_@hotmail.com

    #1

    Problem With Basic Vector Sort

    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(voi d)
    {
    }

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

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

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

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

    void Card::SortCardV ector(vector<Ca rd> targetVector)
    {
    sort(targetVect or.begin(), targetVector.en d(), value);
    }

  • Neelesh

    #2
    Re: Problem With Basic Vector Sort

    A_StClaire_@hot mail.com wrote:[color=blue]
    > 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::SortCardV ector(vector<Ca rd> targetVector)
    > {
    > sort(targetVect or.begin(), targetVector.en d(), value);
    > }[/color]

    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.

    Comment

    • John Harrison

      #3
      Re: Problem With Basic Vector Sort

      Neelesh wrote:[color=blue]
      > A_StClaire_@hot mail.com wrote:
      >[color=green]
      >>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
      >>SortCardVecto r function...?
      >>
      >>Card.h:
      >>
      >>void Card::SortCardV ector(vector<Ca rd> targetVector)
      >>{
      >> sort(targetVect or.begin(), targetVector.en d(), value);
      >>}[/color]
      >
      >
      > 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.
      >[/color]

      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(targetVect or.begin(), targetVector.en d());
      }

      john

      Comment

      • Howard

        #4
        Re: Problem With Basic Vector Sort


        <A_StClaire_@ho tmail.com> wrote in message
        news:1131771967 .894675.246330@ f14g2000cwb.goo glegroups.com.. .[color=blue]
        > 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;
        >[/color]

        This isn't the problem you're seeing, but you're also doing something here
        you shouldn't:
        [color=blue]
        > using namespace std;[/color]

        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



        Comment

        Working...