Hi,
I have a compilation error with the list sort(Comp). Following is the description of the error:
error C2664: 'void __thiscall std::list<struct people,class std::allocator<struct people> >::sort(struct std::greater<struct people>)' : cannot convert parameter 1 from 'bool (const struct people &,const struct people &)' to 'struct std::greater<struct people>'
No constructor could take the source type, or constructor overload resolution was ambiguous - ********************************************
-
//my code
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <list>
-
-
-
using namespace std;
-
-
typedef struct people
-
{
-
int height;
-
int weight;
-
} people;
-
-
-
bool compare_height(const people& first, const people& second)
-
{
-
if (first.height > second.height)
-
return true;
-
else
-
return false;
-
-
}
-
-
bool compare_weight(const people& first, const people& second)
-
{
-
if (first.weight > second.weight)
-
return true;
-
else
-
return false;
-
-
}
-
-
void print(list<people>);
-
-
-
int main(void)
-
{
-
int count = 0;
-
-
list<people> group;
-
people a;
-
-
printf("please input the data \n");
-
-
scanf("%d, %d", &a.height, &a.weight);
-
-
while(a.height != 0){
-
group.push_back(a);
-
scanf("%d, %d", &a.height, &a.weight);
-
}
-
-
print(group);
-
-
printf("after soring \n");
-
group.sort();
-
print(group);
-
-
-
printf("after soring with height\n");
-
group.sort(compare_height);
-
print(group);
-
-
printf("after soring with weight\n");
-
group.sort(compare_weight);
-
print(group);
-
-
return 0;
-
}
-
-
void print(list<people> group)
-
{
-
for(list<people>::iterator i = group.begin(); i != group.end(); ++i){
-
people m = *i;
-
printf("%d, %d \n", m.height, m.weight);
-
}
-
}
-
-
***********************************************
THANK YOU.
7 2941
list::sort requires an operator< for people. In order to sort a call is made to a function that returns true if the frst argument is less than the second argument.
You need to write: - bool operator<(people& lhs, people& rhs)
-
{
-
//TODO: the guts
-
}
or don't use list::sort but use std::sort instead because there you can specify your own comparator function which means you don't need to name it operator<.
Thank you.
It seems only allowing one sort function. The default sort() does not work after I overloaded the function. Here is the new code after change. - ************************************************
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <list>
-
-
-
using namespace std;
-
-
typedef struct people
-
{
-
int height;
-
int weight;
-
} people;
-
-
-
bool operator>(const people& first, const people& second)
-
{
-
if (first.height > second.height)
-
return true;
-
else
-
return false;
-
-
}
-
-
-
-
void print(list<people>);
-
-
-
int main(void)
-
{
-
int count = 0;
-
-
list<people> group;
-
people a;
-
-
printf("please input the data \n");
-
-
scanf("%d, %d", &a.height, &a.weight);
-
-
while(a.height != 0){
-
group.push_back(a);
-
scanf("%d, %d", &a.height, &a.weight);
-
}
-
-
print(group);
-
-
-
printf("after soring with height\n");
-
group.sort( greater<people>());
-
print(group);
-
-
-
return 0;
-
}
-
-
void print(list<people> group)
-
{
-
for(list<people>::iterator i = group.begin(); i != group.end(); ++i){
-
people m = *i;
-
printf("%d, %d \n", m.height, m.weight);
-
}
-
}
-
*************************************************
list::sort calls your operator< not your operator>.
Thank you, weaknessforcats.
Could you elaborate a little more of your last post?
weaknessforcats, I guess you mean I should change the code in the compare function to be
if (first.height < second.height)
instead of
if (first.height > second.height)
What I mean is that list::sort has this code: - if (left < right)
-
{
-
etc...
That is a call to operator<() that returns true if left is less that right.
It makes no difference what the code inside operator<() does. For example, if your operator<() returns true if 9 is less than 3 you will find that you have a sort in descending sequence. Otherwise, you have a sort in ascending sequence.
So, your operator<() returns true based on how you define "lessness". Many of the STL containers n algorithms let you pass a function pointer to a comparator function that acts like an operator<(). Therefore, you can have all manner of ascending, descending sorts, including those with inner and outer keys so long as you have a comparator function that knows what "less" means today.
Thank you.
It is working now.
bool operator<(const people& first, const people& second)
{
if (first.height < second.height)
return true;
else
return false;
}
bool operator>(const people& first, const people& second)
{
if (first.height > second.height)
return true;
else
return false;
}
int main()
{
...
group.sort( );
group.sort( greater<people>());
...
}
Thank you so much for the detail explanation of sort comparison function!
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: RedEagle |
last post by:
Hi All!
Do you remember me? I am that desperate who had this error for a
while:
---
Compilation Error
Description: An error occurred during the compilation of a resource
required to service...
|
by: Yan |
last post by:
Here is the code:
class A {};
void (A::*A) (); // Line 3
int main() {
A a; // Line 6
return 0;
}
|
by: sam_cit |
last post by:
Hi,
I just have the simple lex program
%%
. ECHO;
%%
int main()
{
yylex();
}
|
by: thuang2 |
last post by:
Hi,
Myfile.c include a "Otherfile.h" and this file is under different
directory. When I compile Myfile.c, the compiler says "Otherfile.h: No
such file or directory" I include the path to...
|
by: sunny |
last post by:
We have three files a.c, b.c and main.c respectively as follows:
a.c
---
int a;
b.c
---
int a = 10;
main.c
------
extern int a;
| |
by: Gary Wessle |
last post by:
Hi
I am trying to return a derived type pointer as the base type pointer
and the compiler is complaining, here is what I mean.
class A {
public:
A(){}
virtual void lol()=0;
};
|
by: subramanian |
last post by:
Consider the following program:
#include <iostream>
#include <string>
class Member {
int x;
int y;
public:
Member(int argx, int argy);
|
by: Larry |
last post by:
I was testing the buffer size of system call, read(), and found a
strange error on Ubuntu 7.10 server. The code is attached.
If the BUFFSIZE is set to from 128 to 255, the code will produce an...
|
by: Chris Peters |
last post by:
Hi,
I want to make the delete() operator private for my class - I'm using
reference counts, so I want to force users of my class to call my function
rather than being able to delete and confuse...
|
by: markryde |
last post by:
Hello,
Followed here is a simplified code example of something which I
try to implement; in essence , I want to assign a value to a return
value of a method is C. I know, of course, that in this...
|
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,...
| |
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: 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...
|
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...
|
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,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |