469,646 Members | 1,739 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

compilation error with sort(Comp)

14
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 2733
weaknessforcats
9,208 Expert Mod 8TB
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
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 Expert Mod 8TB
list::sort calls your operator< not your operator>.
Feb 22 '11 #4
June Ye
14
Thank you, weaknessforcats.
Could you elaborate a little more of your last post?
Feb 24 '11 #5
June Ye
14
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 Expert Mod 8TB
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
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

Post your reply

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

Similar topics

2 posts views Thread by sam_cit | last post: by
21 posts views Thread by thuang2 | last post: by
14 posts views Thread by sunny | last post: by
3 posts views Thread by Gary Wessle | last post: by
2 posts views Thread by subramanian | last post: by
9 posts views Thread by Larry | last post: by
4 posts views Thread by Chris Peters | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.