Problème list::sort et classe
Bonjour,
J'ai défini dans une même classe un tableau tab et une fonction de comparaison comp_decr se servant de ce tableau.
Maintenant, je tente d'utiliser la fonction list::sort avec cette fonction de comparaison (à l'intérieur d'une méthode de la classe), mais apparemment, le compilateur n'aime pas trop.
Voilà mon code:
test_sort.h :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #ifndef TEST_H
#define TEST_H
#include <vector>
using namespace std;
class Classe_sort {
public:
vector<int> tab;
int size;
bool comp_decr(int i, int j); // Fonction de comparaison
void fonction();
};
#endif |
test_sort.cpp :
Code:
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
| #include <vector>
#include <list>
#include <iostream>
#include "Test_sort.h"
using namespace std;
bool Classe_sort::comp_decr(int i, int j) {
return (tab[i]>tab[j]);
}
void Classe_sort::fonction() {
list<int> order;
tab[0] = 2;
tab[1] = 1;
tab[2] = 3;
tab[3] = 0;
size = 3;
for(int i=0;i<4;i++) {
order.push_front(i);
}
// Tri selon les valeurs de tab décroissants
order.sort(comp_decr);
// Affichage de la liste
while (!order.empty()) {
cerr << order.front() << " ";
order.pop_front();
}
cerr << endl;
} |
Et voilà l'erreur rendue par le compilateur:
Test_sort.cpp: In member function ‘void Classe_sort::fonction()’:
Test_sort.cpp:26: error: no matching function for call to ‘std::list<int, std::allocator<int> >::sort(<unresolved overloaded function type>)’
/usr/include/c++/4.4/bits/list.tcc:301: note: candidates are: void std::list<_Tp, _Alloc>::sort() [with _Tp = int, _Alloc = std::allocator<int>]
/usr/include/c++/4.4/bits/list.tcc:378: note: void std::list<_Tp, _Alloc>::sort(_StrictWeakOrdering) [with _StrictWeakOrdering = bool (Classe_sort::*)(int, int), _Tp = int, _Alloc = std::allocator<int>]
Quelqu'un a-t-il une idée pour m'en sortir?