   sorting an array of user-defined struct in c++
  Hi All:I have a problem of sorting an array of user defined struct. The code is like:typedef struct curv_struct{    int ix;    double curv;   curv_struct(int i, double c) {   ix = i; curv = c;    };   bool operator<(curv_struct& p2){   if (curv < p2.curv ) return true;   else return false;    };} curv_pair;bool func(curv_pair& p1, curv_pair& p2){    if (p1.curv < p2.curv ) return true;    else return false;};int main{    vector<curv_pair> array;    array.push_back(curv_pair(1, 3.5) );    array.push_back(curv_pair(5, 2.5) );    array.push_back(curv_pair(2, 1.5) );    sort(array.begin(), array.end()); }However, it gives me numerous errors. I also tried to usesort(array.begin(), array.end(), func);This doesn't work either.The error message is like:/usr/lib64/gcc/x86_64-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algo.h: In function ‘const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&) [with _Tp = curv_struct]’:/usr/lib64/gcc/x86_64-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algo.h:2758:   instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<curv_struct*, std::vector<curv_struct, std::allocator<curv_struct> > >, _Size = long int]’/usr/lib64/gcc/x86_64-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algo.h:2829:   instantiated from ‘void std::sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<curv_struct*, std::vector<curv_struct, std::allocator<curv_struct> > >]’/local/d0p6/ming/Project/CMakeProjects/visvoxel/src/visgeo.cpp:100:   instantiated from here/usr/lib64/gcc/x86_64-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algo.h:91: error: no match for ‘operator<’ in ‘__a < __b’/local/d0p6/ming/Project/CMakeProjects/visvoxel/src/visgeo.cpp:24: note: candidates are: bool curv_struct::operator<(curv_struct&)/usr/lib64/gcc/x86_64-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algo.h:92: error: no match for ‘operator<’ in ‘__b < __c’/local/d0p6/ming/Project/CMakeProjects/visvoxel/src/visgeo.cpp:24: note: candidates are: bool curv_struct::operator<(curv_struct&)/usr/lib64/gcc/x86_64-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algo.h:94: error: no match for ‘operator<’ in ‘__a < __c’/local/d0p6/ming/Project/CMakeProjects/visvoxel/src/visgeo.cpp:24: note: candidates are: bool curv_struct::operator<(curv_struct&)/usr/lib64/gcc/x86_64-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algo.h:98: error: no match for ‘operator<’ in ‘__a < __c’/local/d0p6/ming/Project/CMakeProjects/visvoxel/src/visgeo.cpp:24: note: candidates are: bool curv_struct::operator<(curv_struct&)/usr/lib64/gcc/x86_64-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algo.h:100: error: no match for ‘operator<’ in ‘__b < __c’/local/d0p6/ming/Project/CMakeProjects/visvoxel/src/visgeo.cpp:24: note: candidates are: bool curv_struct::operator<(curv_struct&)Can anyone help me with it please?Thanks   
  bool operator<(curv_struct& p2){Should be:bool operator<(const curv_struct& p2) const{You can also shorten the function to:bool operator<(const curv_struct& p2) const{ return curv < p2.curv; }EDIT: Missed a const...   ;  The problem appears to be with const.You can write your code in two ways (BTW, "typedef struct X { ... } Y;" is not necessary in C++, and you shouldn't end a function in a semicolon):struct curv_pair {    int ix;    double curv;   // Use a "initialiser list" in a constructor    // Google the term for more information    curv_pair(int i, double c)    :   ix(i),   curv(c)    {    }   // Note the "const" reference, and the    // extra const before the function body.    bool operator<(const curv_pair& p2) const {   // simplified condition   return (curv < p2.curv);    }};// ORstruct curv_pair {    int ix;    double curv;   curv_pair(int i, double c)    :   ix(i),   curv(c)    {    }};// You can remove the "inline" if you want to move // the implementation to a separate source file.inline bool operator<(const curv_pair& p1, const curv_pair& p2){   return (p1.curv < p2.curv);};Quote:Original post by rip-offand you shouldn't end a function in a semicolonSorry to be off topic, but really? Why not?   ;  because when classes, structs, enums, etc. end with semicolon and functions not, you can tell apart function ends from struct ends, probably. Also, semicolon is unnecessary after function end (or end of {some code} in general), which alone is a reason enough not to add semicolon here.   ;Quote:Original post by krezQuote:Original post by rip-offand you shouldn't end a function in a semicolonSorry to be off topic, but really? Why not?Well, "shouldn't" might be a bit strong. Its more of a style thing. As Dymtry said, you have already ended the function with the terminating curly brace.Unlike in C, where the typedef struct X { ... } Y idiom is common, in C++ requiring a trailing semicolon on type declarations is actually quite annoying as it is rarely useful. But its just one of many complaints that can be made against C++'s rather eclectic syntax.   