473,772 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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);
}

Nov 22 '05 #1
3 3470
A_*********@hot mail.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::SortCardV ector(vector<Ca rd> targetVector)
{
sort(targetVect or.begin(), targetVector.en d(), 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_*********@hot mail.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
SortCardVecto r function...?

Card.h:

void Card::SortCardV ector(vector<Ca rd> targetVector)
{
sort(targetVect or.begin(), targetVector.en d(), 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(targetVect or.begin(), targetVector.en d());
}

john
Nov 22 '05 #3

<A_*********@ho tmail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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
2079
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
3504
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< ValuePair_t, ValuePair_t, bool> ValuePair_IsLess; void SortAscend(const ValuePair_IsLess& isLess_) {
4
1916
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
16604
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
1947
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 Assembler.Why bother with C++?
0
2607
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 monomers) which are represented as doubly pointed noncomplete binary trees. There are three different types of monomers (hence the three different constructer calling functions) the first one is the "leaves" of the tree, the second adds length...
4
2388
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
3290
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, except when used with STL's random_shuffle(). The following code demonstrates a working call (on sort()) and a call that generates an error (on random_shuffle()), both using the same technique. The code: #include <algorithm>
14
2088
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 criteria of sorting. Basically, I would like to sort on visitor count of the Attraction structure. However, it never uses the < overloaded operator with my code. Handler.h:
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10264
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.