473,503 Members | 10,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compilation error with sort(Comp)

14 New Member
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

Expand|Select|Wrap|Line Numbers
  1. ********************************************
  2. //my code
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <list>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. typedef struct people
  11. {
  12.     int height;
  13.     int weight;
  14. } people;
  15.  
  16.  
  17. bool compare_height(const people& first, const people& second)
  18. {
  19.     if (first.height > second.height)
  20.         return true;
  21.     else
  22.         return false;
  23.  
  24. }
  25.  
  26. bool compare_weight(const people& first, const people& second)
  27. {
  28.     if (first.weight > second.weight)
  29.         return true;
  30.     else
  31.         return false;
  32.  
  33. }
  34.  
  35. void print(list<people>);
  36.  
  37.  
  38. int main(void)
  39. {
  40.     int count = 0;
  41.  
  42.     list<people> group;
  43.     people a;
  44.  
  45.     printf("please input the data \n");
  46.  
  47.     scanf("%d, %d", &a.height, &a.weight);
  48.  
  49.     while(a.height != 0){
  50.         group.push_back(a);
  51.         scanf("%d, %d", &a.height, &a.weight);
  52.     }
  53.  
  54.     print(group);
  55.  
  56.     printf("after soring \n");
  57.     group.sort();
  58.     print(group);
  59.  
  60.  
  61.     printf("after soring with height\n");
  62.     group.sort(compare_height);
  63.     print(group);
  64.  
  65.     printf("after soring with weight\n");
  66.     group.sort(compare_weight);
  67.     print(group);
  68.  
  69.     return 0;
  70. }
  71.  
  72. void print(list<people> group)
  73. {
  74.     for(list<people>::iterator i = group.begin(); i != group.end(); ++i){
  75.             people m = *i;
  76.             printf("%d, %d \n", m.height, m.weight);
  77.     }
  78. }
  79.  
  80. ***********************************************
THANK YOU.
Feb 18 '11 #1
7 2941
weaknessforcats
9,208 Recognized Expert Moderator Expert
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:

Expand|Select|Wrap|Line Numbers
  1. bool operator<(people& lhs, people& rhs)
  2. {
  3.     //TODO: the guts
  4. }
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<.
Feb 20 '11 #2
June Ye
14 New Member
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.


Expand|Select|Wrap|Line Numbers
  1. ************************************************
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <list>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. typedef struct people
  10. {
  11.     int height;
  12.     int weight;
  13. } people;
  14.  
  15.  
  16. bool operator>(const people& first, const people& second)
  17. {
  18.     if (first.height > second.height)
  19.         return true;
  20.     else
  21.         return false;
  22.  
  23. }
  24.  
  25.  
  26.  
  27. void print(list<people>);
  28.  
  29.  
  30. int main(void)
  31. {
  32.     int count = 0;
  33.  
  34.     list<people> group;
  35.     people a;
  36.  
  37.     printf("please input the data \n");
  38.  
  39.     scanf("%d, %d", &a.height, &a.weight);
  40.  
  41.     while(a.height != 0){
  42.         group.push_back(a);
  43.         scanf("%d, %d", &a.height, &a.weight);
  44.     }
  45.  
  46.     print(group);
  47.  
  48.  
  49.     printf("after soring with height\n");
  50.     group.sort( greater<people>());
  51.     print(group);
  52.  
  53.  
  54.     return 0;
  55. }
  56.  
  57. void print(list<people> group)
  58. {
  59.     for(list<people>::iterator i = group.begin(); i != group.end(); ++i){
  60.         people m = *i;
  61.         printf("%d, %d \n", m.height, m.weight);
  62.     }
  63. }
  64. *************************************************
Feb 20 '11 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
list::sort calls your operator< not your operator>.
Feb 22 '11 #4
June Ye
14 New Member
Thank you, weaknessforcats.
Could you elaborate a little more of your last post?
Feb 24 '11 #5
June Ye
14 New Member
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)
Feb 24 '11 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
What I mean is that list::sort has this code:

Expand|Select|Wrap|Line Numbers
  1. if (left < right)
  2. {
  3. 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.
Feb 26 '11 #7
June Ye
14 New Member
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!
Feb 28 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

10
9355
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...
6
1888
by: Yan | last post by:
Here is the code: class A {}; void (A::*A) (); // Line 3 int main() { A a; // Line 6 return 0; }
2
13567
by: sam_cit | last post by:
Hi, I just have the simple lex program %% . ECHO; %% int main() { yylex(); }
21
1620
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...
14
1669
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;
3
266
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; };
2
1547
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);
9
1377
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...
4
274
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...
11
2483
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...
0
7207
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
7361
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...
1
7015
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...
0
7470
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...
0
5602
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,...
0
4693
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...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1523
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 ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.