J'essaie d'imprimer un tableau d'entiers. Je reçois une faute de seg quand j'essaie d'imprimer comme ci-dessous. Si je décommente le "In for loop" il imprimera tout sauf le dernier élément du tableau et il a toujours un défaut seg. Quand je décommente les deux commentaires (ou juste le "fait avec la boucle") tout imprime bien. Pourquoi cela arrive-t-il et comment puis-je le réparer?Erreur de segmentation lors de la sortie en C++
for(int i = 0; i < l.usedLength; i++)
{
//cout << "**********In for loop" << endl;
cout << l.largeInt[ i ];
}
//cout << "**********done with for loop" << endl;
est ici toute la classe:
#include "LargeInt.h"
#include <ctype.h>
LargeInt::LargeInt()
{
usedLength = 0;
totalLength = 50;
largeInt = new int[totalLength];
for(int i=0; i<totalLength; i++)
{
largeInt[i] = 0;
}
}
LargeInt LargeInt::operator+(const LargeInt &l) const
{}
LargeInt LargeInt::operator-(const LargeInt &l) const
{}
LargeInt LargeInt::operator*(const LargeInt &l) const
{}
LargeInt LargeInt::operator/(const LargeInt &l) const
{}
bool LargeInt::operator==(const LargeInt &l) const
{}
ostream& operator<<(ostream &out, const LargeInt &l)
{
cout << "In output" << endl;
if(l.usedLength == 0)
{
cout << 0;
}
else
{
cout << "In else... NON 0" << endl;
for(int i = 0; i < l.usedLength; i++)
{
cout << "In for loop" << endl;
cout << l.largeInt[ i ];
}
//cout << "done with for loop" << endl;
}
//cout << "after the if.... all done with output" << endl;
}
istream& operator>>(istream &in, LargeInt &l)
{
char x;
while (std::cin.get(x) && x >= '0' && x <= '9')
{
l.largeInt[ l.usedLength ] = x-48;
l.usedLength++;
//need to check array length and make bigger if needed
}
}
principal:
#include <stdlib.h>
#include <iostream>
#include "LargeInt.h"
int main(int argc, char** argv) {
cout << "\nJosh Curren's Assignment #5 - Large Integer\n" << endl;
LargeInt lint;
cout << "Enter a large int: ";
cin >> lint;
cout << "\nYou entered: " << endl;
cout << lint << endl;
cout << endl;
return (EXIT_SUCCESS);
}
Quoi de neuf? 15 – Tom
quel est le type de 'l'? – Vlad
Pourriez-vous décrire d'autres? Quel type est de l? Qu'est-ce que l.usedLength, l.largeInt? – lollinus