I'm new to C++ and the memory nuances that are needed to write and debug the language. Can anyone tell me why the following code is giving me a segmentation fault?
string Polynomial::toString(){
int i, exponent;
stringstream result;
for (i = 0; i < coeffs.size(); i++){
// For first non-zero coefficient
if (result.str().empty()){
if(coeffs[i] < 0)
result << "-";
if(coeffs[i] != 0)
result << coeffs[i];
}
else{
if(coeffs[i] < 0)
result << " - " << abs(coeffs[i]);
else if(coeffs[i] > 0)
result << " + " << coeffs[i];
}
exponent = (coeffs.size() - i - 1);
if (coeffs[i] != 0){
if (exponent > 1)
result << coeffs[i] << "x^" << exponent;
else if(exponent == 1)
result << coeffs[i] << "x";
}
}
result.str();
}
coeffsdefined, and how do you populate it with values?g++ -Wall -gon Linux) and learn to use the debugger (gdbon Linux).