Bonjour tout le monde J'ai une question pour les <templates>
Voila. J'ai ecrit une classe template qui permet de créer et afficher des tableaux de "choses" ( int, float, char *, tableaux tableaux de int, etc...) J'ai un probleme d'affichage pour un tableau des tableaux des char *.
Je mets le code, pour que ca soit plus clair.
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <cstdlib>
#include <stdio.h> 
 
using namespace std;
 
template <class T>
class Tableau 
{
private:
	int taille;
	T *  donnee;
	int verif(int i) const    
	{ 
		if (i < 0 || i >= taille)  cout << "Erreur" ;
		return i; 
	}
 
public:
 
	Tableau(int len = 10) : taille(len), donnee(new T[len])  
	{ }
	~Tableau()
	{
		delete [] donnee;
	}
 
	int len() const			
	{
		return taille;
	}
 
	const T& operator[](int i) const
	{
		return donnee[verif(i)];
	}
 
	T& operator[](int i)
	{
		return donnee[verif(i)];
	}
 
	Tableau(const Tableau<T>&);    
 
	Tableau<T>& operator= ( Tableau<T> & t)  
	{
		this->taille = t.taille;
		this->donnee = new T[ t.taille]; 
		for(int i = 0; i < this->taille; i++)
		{
			donnee[i] = t[i];
		}
		return  *this;
	}
 
	void  afficher()
	{
		cout << *this;
		cout << endl;
	}
 
	void  afficher_Tab_char()
	{
		for(int i = 0; i < this->len(); i++)
		{
			//if(  (*this) [i] )
			cout << " indice [" << i << "] " << (*this)[i];
 
		}
		cout << endl;
	}
 
 
	friend ostream & operator << (ostream & os,  Tableau<T> & t)
	{
		for(int i = 0; i < t.taille; i++)
		{
			os << t.donnee[i] << ",";
		}
		return os << endl;
	}
 
};
template< >	//  pour surcharger pour les char *				
Tableau<char *>& Tableau<char *>::operator= ( Tableau <char*> & t)   
{
	typedef char * ttt;
	delete[] donnee;
	this->donnee = new ttt[ t.taille]; 
 
	for(int i = 0; i < t.len(); i++)
	{
		donnee[i] = new char[ strlen(t[i])+1];
		strcpy( this->donnee[i], t.donnee[i]);
	}
	return * this;
}
 
 
//===============================================================================
void main()
 
{
 
	Tableau <int>  tab_int;				
              for(int i = 0; i < tab_int.len(); i++)	
	{
		tab_int[i] = 10+i;
	}
 
	cout  << tab_int;  
	Tableau <int> tab_int2;
	tab_int2 = tab_int; 
	cout  << tab_int2;
 
	tab_int2[9]=5555;     
 
	Tableau< Tableau<int> >  tab_tab(3);		
	tab_tab[0] = tab_int;
	tab_tab[1] = tab_int2;
	tab_tab[2] = tab_int;
	cout << tab_tab;
	tab_tab.afficher_Tab_char();
 
 
	Tableau<char*> tab_char(5);
 
	tab_char[0] = "Coucou0";
	tab_char[1] = "Coucou1";
	tab_char[2] = "Coucou2";
	tab_char[3] = "Coucou3";
	tab_char[4] = "Coucou4";
 
	tab_char.afficher();
 
	Tableau<char*>   tab_char2(5);			
	tab_char2 = tab_char;
	tab_char2[1] = "Salut";
 
	cout << "Tab char * (2)  :\n";
	tab_char2.afficher();
 
	Tableau< Tableau<char *> >  tab_tab_char(3);
	tab_tab_char[0]= tab_char;
	tab_tab_char[1]= tab_char;
	tab_tab_char[2]= tab_char;
 
 
}

je voudrai pouvoir ecrire ça : ou utiliser la methode afficher()



Désolé pour le long code...
Merci d'avance, si qqn aura une idée