probleme avec une boucle infinie
:salut: j'étais entrain de m'exercer sur la surcharge des opérateurs << et le ++ postfixé et préfixe quand j'ai rencontré le problème suivant:
j'ai crée la classe suivante:
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 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
| class tableau
{
int * elmt;
const int taille;
public:
tableau();
tableau(int *e_,int t_);
tableau(const tableau&);
~tableau();
friend void taille(const tableau &t);
int& operator [](const int);
int& operator [](const int)const;
tableau& operator++();
tableau& operator++(int);
friend ostream& operator<<(ostream &o,const tableau &t);
};
tableau::tableau():taille(1)
{
elmt=new int[taille];
}
tableau::tableau(int *e,int t_):taille(t_)
{
elmt=new int[taille];
for (int i=0;i<taille;i++)
{
elmt[i]=e[i];
}
}
tableau::tableau(const tableau& orgnl):taille(orgnl.taille)
{
elmt=new int[orgnl.taille];
for (int i=0;i<orgnl.taille;i++)
{
elmt[i]=orgnl.elmt[i];
}
}
tableau::~tableau()
{
delete elmt;
}
int& tableau::operator[](const int ind)
{
if (ind>taille ||ind<0)
{
throw("probleme avec indice\n!");
}
return elmt[ind];
}
int& tableau::operator[](const int ind)const
{
if (ind>taille ||ind<0)
{
throw("probleme avec indice\n!");
}
return elmt[ind];
}
//post fixé
tableau& tableau::operator++()
{
for (int i=0;i<taille;i++)
{
++elmt[i];
}
return *this;
}
//prefixé
tableau& tableau::operator++(int)
{
tableau temp=*this;
for (int i=0;i<taille;i++)
{
elmt[i]++;
}
return temp;
} |
et lorsque j'execute ce code ça boucle indefiniment puis ça plante
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 26 27 28 29 30 31 32 33
|
ostream& operator<<(ostream &o,const tableau &t)
{
for (int i=0;i<t.taille;i++)
{
o<<"elmt["<<i<<"]"<<t.elmt[i]<<endl;
}
return o;
}
void taille(const tableau &t)
{
cout<<"taille"<<t.taille<<endl;
}
int main()
{
int temp[5]={1,2,3,4,5};
tableau t(temp,5);
tableau c=t;
cout<<c++;//le probleme se situe ici dans l'incrementation postfixé
//si je remplace la ligne en dessus par celles en dessous ça marche sans
//probleme
c++;
cout<<c;
return 0;
} |
Merci d'avance .