私は C++ と、言語の作成とデバッグに必要なメモリのニュアンスに不慣れです。次のコードでセグメンテーション違反が発生する理由を誰か教えてもらえますか?
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();
}