473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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(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 22 '05 #1
3 3451
A_*********@hotmail.com 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.

Nov 22 '05 #2
Neelesh wrote:
A_*********@hotmail.com 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
Nov 22 '05 #3

<A_*********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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
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

Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rachel Forder | last post by:
Hi All, I have a problem related to the sort function provided by STL. class A{ A(string, string, int); string itemA; string itemB; int itemC; };
8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
4
by: Johan | last post by:
Hi, Why does my vector not sort. What I understand is you have to overload the < operator, but that does not work. see code below Thanks Johan
8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
4
by: Gerry Lintonice | last post by:
I wonder if C++ is any better than a common BASIC, for example GW BASIC. What can you do with C++ that BASIC can't do? C++ seems so incredibly complicated, even more complicated than...
0
by: rokuingh | last post by:
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated...
4
by: Gaijinco | last post by:
I'm not quite sure why this code doesn't works: #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; class word {
2
by: Nelis Franken | last post by:
Good day. Thanks for the previous help on binding member functions to use as predicates for STL functions (original example applied to sort()). The technique to use Boost's bind() works well,...
14
by: Frank | last post by:
Hello everyone, I am having trouble overloading the < operator for an assignment. I use a struct that contains information and I would like to sort this structure using STL sort with my own...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.