我有重載的operator +和流< <問題。我有類重載操作符:重載流和算術運算問題
FuzzyNumber& FuzzyNumber::add(FuzzyNumber B)
{
FuzzyNumber fuzzyResult;
fuzzyResult.setA(this -> getA() + B.getA());
fuzzyResult.setB(this -> getB() + B.getB());
fuzzyResult.setC(this -> getC() + B.getC());
return fuzzyResult;
}
FuzzyNumber& operator+(FuzzyNumber& A, FuzzyNumber& B)
{
return A.add(B);
}
All fields inside class are double type. Here is overloaded ostream operator>>
ostream& FuzzyNumber::streamWrite(ostream& outStream)
{
outStream << "("
<< this -> getA() << ", "
<< this -> getB() << ", "
<< this -> getC() << ")";
return outStream;
}
ostream& operator<< (ostream& outStream, FuzzyNumber& fuzzyNumber)
{
fuzzyNumber.streamWrite(outStream);
return outStream;
}
它是打算打印類似的東西(3,4.3,5.1)當我COUT鍵入< < fuzzyNumber ;.它工作正常,而且這也能正常工作:
FuzzyNumber fuzzyNumber = numA + numB;
cout << "A + B = " << fuzzyNumber << endl;
其中numA
和numB
是FuzzyNumber類型。 儘管程序停止的時候我這個替換上面一行運行:
cout << "A + B = " << (numA + numB) << endl;
也許這是什麼毛病默認運算符=,但目前還沒有動態變量在這個類中,所以它不應該。
感謝您的幫助!
定義「停止運行」。你的意思是它崩潰了嗎?鎖定?不編譯? – 2010-11-16 18:00:40
「停止運行」是什麼意思? – Cascabel 2010-11-16 18:01:13