Velocity Reviews > C++ > Problem With Basic Vector Sort

# Problem With Basic Vector Sort

A_StClaire_@hotmail.com
Guest
Posts: n/a

 11-12-2005
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);
}

Neelesh
Guest
Posts: n/a

 11-12-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

John Harrison
Guest
Posts: n/a

 11-12-2005
Neelesh wrote:
> (E-Mail Removed) 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)
{
// 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

Howard
Guest
Posts: n/a

 11-12-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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

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