Sorting a vector of structs

Hi, I am trying to sort a vector that contains pointers to structs. I read a lot in the internet and in this forum about that. However I do not know what I do wrong.

Here are the parts in the code that the problem concernes:

Where I try to call the sort:
void structure::neighbours() {
sort(Cross.begin(),Cross.end(),OrderCrossings);
}

The OrderCrossings function:
bool structure::OrderCrossings(const crossing *a,const crossing *b) {
return (b->c[0]>a->c[0]);
}

The struct:
struct crossing {
double *c;
int *f;
int *Neigh;};

In the constructor for structure c becomes a new double array with 2 entres and f becomes a new int with 2 entries.
Last edited on
What is wrong with it?
error: no matching function for call to 'sort(std::vector<crossing*>::iterator, std::vector<crossing*>::iterator, <unresolved overloaded function type>)'|
c:\mingw\bin\..\lib\gcc\mingw32\4.5.0\include\c++\bits\stl_algo.h|5236|

note: candidate is: void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<crossing**, std::vector<crossing*> >, _Compare = bool (structure::*)(const crossing*, const crossing*)]|
||=== Build finished: 2 errors, 0 warnings ===|

I have included algorithm, so the library should be there.
The 'OrderCrossings()' function must be static or global (not part of a class/struct)
Thanks, for this information. I havn't used static member functions so far, but I tried to use them now. The problem is now that it results in an undefined reference. What do I need to add in order to not get a linker error?

void structure::neighbours() {
sort(Cross.begin(),Cross.end(),structure::OrderCrossings);
}

static bool OrderCrossings(const crossing *a,const crossing *b) {
return (b->c[0]>a->c[0]);
}


The error: undefined reference to `structure::OrderCrossings(crossing const*, crossing const*)'|
||=== Build finished: 1 errors, 0 warnings ===|
If OrderCrossings is a static member of class structure, you need

1
2
3
4
5
6
7
8
9
10
11
12
class structure
{
    ...

    vector<crossing*> Cross;

    void neighbours();

    static bool OrderCrossings(const crossing *a,const crossing *b);

    ...
};


1
2
3
void structure::neighbours() {
    sort(Cross.begin(),Cross.end(), OrderCrossings);
}


and

1
2
3
/*static*/ bool structure::OrderCrossings(const crossing *a,const crossing *b) {
    return (b->c[0]>a->c[0]);
}


Your OrderCrossing looks like a C-style static function.
Last edited on
Thanks a lot to all of you, I was able to solve it now.
Topic archived. No new replies allowed.