std::sort... problems to use this funtion

Dear all,

I have a huge problem by trying to use this funtion. I called this funtion from my main:

1
2
3
4
5
/* Generate random initial population */
       vector<QNode*> * population = generateRandomPopulation(POPULATION);
       
       /* Sort the population and find the global best individual */
       sort(population->begin(), population->end(), DescendingSort());


my DescendingSort() function, I write the following way:

1
2
3
4
5
6
7
8
/* Functor used by std::sort */
struct DescendingSort
{
  bool operator () (QNode *& start, QNode*& end)
   {
   	return start->getValue() < end->getValue();
   }
};


and finally, within my own class definition of QNode, I overwrite the operator functions and between other functions, I have also the getValue() function and the same with a constant suffix:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class QNode
{
privat:
...
public:
/* Assignment Operator */
      QNode & operator = (const QNode & copy)
      {
      	if(assignments != NULL)
         	delete [] assignments;

         assignments = new int[NUMBER_OBJECTS];
         int * as = copy.getAssignments();
         for(int i=0; i<NUMBER_OBJECTS; i++)
         {
         	assignments[i] = as[i];
         }

         calculateValue();
      }

      /* Operator '<' Used by std::sort */
      bool operator < (const QNode & right) const
      {
      	int av = this->getValue();
	int bv = right.getValue();
	return av < bv;
      } 
  
  
  /* Operator '>' */
    bool operator > (const QNode & right) const
  {
    int av = this->getValue();
    int bv = right.getValue();
    return av > bv;
  }

 /* Get the objective function value of this solution */
      int getValue()
      {
      	return value;
      }

  /* Get the objective function value of this solution */
      int getValue() const
      {
      	return value;
      }

...



my problem right now is, that there seems to be somehow a difference to the calling descendingSort function from std::sort and that one what I offer (at least it needs somehow constant variables. By compiling my sourcecode, this is the resulting error:

/usr/include/c++/4.4/bits/stl_algo.h:124: error: no match for call to ‘(DescendingSort) (QNode* const&, QNode* const&)’
Heuristic-ILS.hpp:438: note: candidates are: bool DescendingSort::operator()(QNode*&, QNode*&)


So, when I understand it right, then std::sort needs to call a funtion DescendingSort(..) with constant parameter variables, but unfortunately, I have no idea how to handle or fix this error.

So please, help me! That would be great.
When you create your DescendingSort() object as a temporary (without assigning it to a variable) it is a const. So its function operator needs to be declared const. Also you might want to declare the parameters as const to allow the DescendingSort object to be used with vectors of const pointers.

Try something like this:
1
2
3
4
5
6
7
8
/* Functor used by std::sort */
struct DescendingSort
{
	bool operator()(const QNode* start, const QNode*  end) const
	{
		return start->getValue() < end->getValue();
	}
};
Thank you very much... It compiles and also run right now without a problem anymore.
Topic archived. No new replies allowed.