Re: [問題] C++ 矩陣輸入值在做完+法*法後被改了

看板Programming作者 (pony)時間14年前 (2009/10/18 22:28), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
※ 引述《abyss781118.bbs@bbs.cs.nctu.edu.tw (東東)》之銘言: : 寫了個矩陣相加相乘的class : 假設輸入a,b兩矩陣 : 在做完a+b或a*b時 : a的值就被改掉 : cout<<a; : answer=(a+b); : cout<<a; : 前後出來的a不同 : 接著去看operator+ : 在return前 cout<<*this; 出來看 : cout<<*this; : return answer; : this沒被變動 你改寫一下 operator 看看 class Matrix; Matrix operator + (const Matrix& m) const; 改這個確保不會變動 (a+b) 的 a, b值 const Matrix& operator = (const Matrix& m); 這個不會改變 answer reference 跟 (a+b) 的值 : 後來又去看了operator= : SparseMatrix& SparseMatrix::operator=(const SparseMatrix& rhs) : 在return之前把*this跟rhs cout出來看 : cout<<*this<<endl<<rhs; : return *this; : 都沒有錯 : 那a到底在哪裡被改到?? : 懇請板上的大大為小弟解惑 : 感謝 這樣的話應該就是 cout << 動到了 嘗試寫一個 member function print 代替 cout 看看 int Matrix::print() const { // do what you want } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 125.225.98.138 ※ 編輯: pponywong 來自: 125.225.98.138 (10/18 22:34)
文章代碼(AID): #1AsoOXps (Programming)
文章代碼(AID): #1AsoOXps (Programming)