Bonjour,
Je cherche à integrer un template dans plusieurs structures imbriquées.
Mais le résultat ne correspond pas à ce que j'attends...
Résultats :
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 Résultat attendu : un.id = 1 deux.id = 1 trois.id = 1 #1 trois.id = 7 #2 deux.id = 2 trois.id = 8 #3 deux.id = 4 trois.id = 5 #4 Résultat obtenu : un.id = 1 deux.id = 1 trois.id = 1 trois.id = 7 deux.id = 2 trois.id = 1 trois.id = 7 trois.id = 8 deux.id = 4 trois.id = 1 trois.id = 7 trois.id = 8 trois.id = 5
Manager.h :
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
46
47 #include <iostream> #include "Structure.h" using namespace std; int main(int argc, char** argv){ struct un un; struct deux deux; struct trois trois; un.id = 1; /*#1*/ deux.id = 1; trois.id = 1; deux.mgr.add(trois); /*#2*/ trois.id = 7; deux.mgr.add(trois); un.mgr.add(deux); /*#3*/ deux.id = 2; trois.id = 8; deux.mgr.add(trois); un.mgr.add(deux); /*#4*/ deux.id = 4; trois.id = 5; deux.mgr.add(trois); un.mgr.add(deux); /* AFFICHAGE */ cout << endl; cout << "un.id = " << un.id << endl; for( int i=0 ; i<un.mgr.length ; i++ ){ cout << "\tdeux.id = " << un.mgr.myList[i].id << endl; for( int j=0 ; j<un.mgr.myList[i].mgr.length ; j++ ){ cout << "\t\ttrois.id = " << un.mgr.myList[i].mgr.myList[j].id << endl; } } cout << endl; return 0; }
Structure.h :
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
46
47
48
49
50
51
52 #ifndef MANAGER_H_INCLUDED #define MANAGER_H_INCLUDED #include <stdlib.h> template <class T> class Manager { private: int maximum; public: int length; T* myList; Manager(void); ~Manager(void); void add(T&); }; template <class T> Manager<T>::Manager(void) { length = 0; maximum = 100; myList = (T*)malloc(sizeof(T)*maximum); //for (int i=0 ; i<maximum ; i++) myList[i] = NULL; return; } template <class T> Manager<T>::~Manager(void) { length = 0; maximum = 0; free(myList); return; } template <class T> void Manager<T>::add(T &obj) { if (length==maximum){ maximum *= 2; myList = (T*)realloc(myList, sizeof(T)*maximum); } myList[length++] = obj; return; } #endif //MANAGER_H_INCLUDED
Quelqu'un pourrait t'il m'aider ?
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 #ifndef STRUCTURE_H_INCLUDED #define STRUCTURE_H_INCLUDED #include "Manager.h" typedef struct trois { int id; //... } trois; typedef struct deux { int id; //... Manager<trois> mgr; } deux; typedef struct deuxBis { int id; //... Manager<trois>* mgr; } deuxBis; typedef struct un { int id; //... Manager<deux> mgr; } un; #endif //STRUCTURE_H_INCLUDED
Merci
Partager