Bonjour,
j'ai un léger problème , j'ai d abords créé une classe CType qui regroupe les 3 types de bases que j'ai pris le soin de rédéfinir , le problème vient quand j'essaie d'utiliser cette classe dans une autre classe pour pouvoir construire une sorte de tableau avec string float int .
Je vous met le code :
de la classe CType
De ma classe CBuildData
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 #include <iostream> #include <sstream> #include <string> using namespace std; class CType { public: CType() { } virtual string toString()= 0; }; class CString : public CType { private : string m_stringVal; public : CString(string p_stringVal) { m_stringVal = p_stringVal; } string toString() { return m_stringVal; } }; class CInteger : public CType{ private : int m_intVal; public : CInteger(int p_intVal) { m_intVal = p_intVal; } string toString() { ostringstream out; out << m_intVal; return out.str(); } }; class CFloat : public CType{ private : float m_floatVal; public : CFloat(float p_floatVal) { m_floatVal = p_floatVal; } string toString() { ostringstream out; out << m_floatVal; return out.str(); } };
Et de mon 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 #include "CType.h" //Gestion de la construction des données class CBuildData { private : CType *data[4]; int taillemax; int cpt; public : CBuildData (int p_taille) { taillemax = p_taille; // data = new CType[taillemax]; cpt = 0; } void addDataInteger(int p_data) { CInteger dataToAdd(p_data); if (cpt < taillemax ) { data[cpt] = &dataToAdd; cpt++; } } void addDataFloat(float p_data) { CFloat dataToAdd(p_data); if (cpt < taillemax ) { data[cpt] = &dataToAdd; cpt++; } } void addDataString(string p_data) { CString dataToAdd(p_data); if (cpt < taillemax ) { data[cpt] = &dataToAdd; cpt++; } } void toPrint() { for (int i=0;i<cpt;i++) { cout << "Donnees " <<i<< " : " << data[i]->toString()<<endl; } } };
Les tests de CType marche avec succès le problème vient de la classe CBuildData lors de l'affichage , mon objet data de la classe CBuildData ne prend en compte que le 9.5 et l'affiche 2 fois alors qu'il devrait afficher successivement 5 , "Bonjour" , 9.5 .
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 #include <cstdlib> #include <iostream> #include "CBuildData.h" using namespace std; int main(int argc, char *argv[]) { //Test CType CInteger test(10); CString test2("bonjour"); CFloat test3(9.5); CType *obj = &test; CType *obj2 = &test2; CType *obj3 = &test3; cout << "String : " << obj2->toString() << endl; cout << "Int : " << obj->toString() << endl ; cout << "Float : " << obj3->toString() << endl; //Test CBuildData CBuildData tryBuild(5); tryBuild.addDataInteger(5); tryBuild.addDataString("Bonjour"); tryBuild.addDataFloat(9.5); tryBuild.toPrint(); system("PAUSE"); return EXIT_SUCCESS; }
Merci de votre aide.
Partager