Bonjour,
je voudrais mettre dans une classe template un vecteur qui vas contenir des objets de type T. Le type que j'aurais défini dans mon main.
Le code ci dessous fonctionne très bien avec des types prédéfini tel que int. Mais dès que je veux utiliser une classe perso, ca ne fonctionne plus.
Dans le main, je ne peux pas ajouter d'objet, j'ai une erreur au moment de la compilation.
Ma classe template !
Le main :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include "Point.h" #include <vector> #include <iostream> #include <stdlib.h> #include <stdio.h> using namespace std; template <class T> class Pile { private : vector<T> tab; public : Pile(); Pile(const vector<T> &t); ~Pile(); void addObject(const T &object); void dellObject(); }; template <class T> Pile<T>::Pile() { cout << "pile construite" << endl; } template <class T> Pile<T>::Pile(const vector<T> &t) { } template <class T> Pile<T>::~Pile() { } template <class T> void Pile<T>::addObject(const T &object) { tab.push_back(object); } template <class T> void Pile<T>::dellObject() { tab.pop_back(); }
Le header de la classe Point (qui fonctionne, je l'ai déjà testée séparément).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include "Point.h" #include "Pile.cpp" #include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; int main() { Point a; Pile<Point> pile_1; pile_1.addObject(a); return 0; }
Pour les plus courageux, mes erreux de compilation :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 POINT #define POINT class Point { private : short x; short y; public : Point(); Point(Point & a); ~Point(); void affiche(); short getX(); short getY(); void setX(short value); void setY(short value); float norme(Point a); bool testEgal(Point a); }; #endif
D'après ce que je comprend je ne donne pas de bon paramètre à la méthode addObject. Mais j'ai beau essayer tous ce qui me passe par la tête, je n'y arrive pas
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 g++ -Wall -O2 main.cpp Point.o Pile.o In file included from /usr/lib/gcc/i686-redhat-linux/4.4.2/../../../../include/c++/4.4.2/i686-redhat-linux/bits/c++allocator.h:34, from /usr/lib/gcc/i686-redhat-linux/4.4.2/../../../../include/c++/4.4.2/bits/allocator.h:48, from /usr/lib/gcc/i686-redhat-linux/4.4.2/../../../../include/c++/4.4.2/vector:62, from Pile.cpp:2, from main.cpp:2: /usr/lib/gcc/i686-redhat-linux/4.4.2/../../../../include/c++/4.4.2/ext/new_allocator.h: In member function void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = Point]: /usr/lib/gcc/i686-redhat-linux/4.4.2/../../../../include/c++/4.4.2/bits/stl_vector.h:737: instantiated from void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Point, _Alloc = std::allocator<Point>] Pile.cpp:38: instantiated from void Pile<T>::addObject(const T&) [with T = Point] main.cpp:15: instantiated from here /usr/lib/gcc/i686-redhat-linux/4.4.2/../../../../include/c++/4.4.2/ext/new_allocator.h:105: erreur: no matching function for call to Point::Point(const Point&) Point.h:12: note: candidats sont: Point::Point(Point&) Point.h:11: note: Point::Point() In file included from /usr/lib/gcc/i686-redhat-linux/4.4.2/../../../../include/c++/4.4.2/vector:69, from Pile.cpp:2, from main.cpp:2: /usr/lib/gcc/i686-redhat-linux/4.4.2/../../../../include/c++/4.4.2/bits/vector.tcc: In member function void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Point, _Alloc = std::allocator<Point>]: /usr/lib/gcc/i686-redhat-linux/4.4.2/../../../../include/c++/4.4.2/bits/stl_vector.h:741: instantiated from void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Point, _Alloc = std::allocator<Point>] Pile.cpp:38: instantiated from void Pile<T>::addObject(const T&) [with T = Point] main.cpp:15: instantiated from here /usr/lib/gcc/i686-redhat-linux/4.4.2/../../../../include/c++/4.4.2/bits/vector.tcc:306: erreur: no matching function for call to Point::Point(const Point&) Point.h:12: note: candidats sont: Point::Point(Point&) Point.h:11: note: Point::Point() make: *** [main] Erreur 1
Si vous avez une idée, merci.
Partager