Bonjour à tous.
Dans le but de m'entraîner, j'ai voulu coder une classe qui imite std::vector. Celle-ci est finie, là n'est pas le problème, mais toujours dans le but de m'entraîner, j'ai voulu implémenter une méthode sort qui trie les éléments du vecteur, et tant qu'à faire, utiliser des classes de politique pour implémenter plusieurs algorithmes de tri.
Malheureusement, cela ne marche pas. Voilà mon code:
vector.h
tab_sort.hCode:
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 #ifndef DEF_VECTOR #define DEF_VECTOR #include "utilities.h" #include "tab_sort.h" namespace myStl { template <typename T> class vector { public: /* ... */ template <typename Tri> void sort() { Tri::sort(tab, taille); } /* ... */ private: value_type* tab; size_type taille; size_type max_taille; }; /* ... */ }
main.cppCode:
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 #ifndef DEF_TAB_SORT #define DEF_TAB_SORT #include "utilities.h" namespace myStl { template <typename T> struct bubbleSort { static void sort(T* v, size_t size) { for(size_t i=1 ; i<size ; i++) { for(size_t j=0 ; j<size-i ;j++) { if(v[j] > v[j+1]) myStl::swap(v[j], v[j+1]); } } } }; } #endif
J'obtiens une magnifique erreur lors de la compilation: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 #include "headers/string.h" #include "headers/vector.h" using namespace myStl; typedef vector<int> t_tab; int main() { t_tab v; for(int i=10 ; i ; i--) v.push_back(i); for(t_tab::iterator it = v.begin() ; it != v.end() ; it++) std::cout << *it << std::endl; std::cout << std::endl; v.sort<bubbleSort>(); /* Probleme a cette ligne */ for(t_tab::iterator it = v.begin() ; it != v.end() ; it++) std::cout << *it << std::endl; return 0; }
Pourtant, il me semble bien avoir déclaré et défini cette fonction :? ...Code:
1
2 myStl\main.cpp:19: error: no matching function for call to `myStl::vector<int>::sort()' :: === Build finished: 1 errors, 0 warnings ===
Bref, j'ai besoin de vos lumières, merci d'avance de votre patience :) .