作者pponywong (pony)
看板Programming
标题Re: [问题] C++ 矩阵输入值在做完+法*法後被改了
时间Sun Oct 18 22:28:47 2009
※ 引述《[email protected] (东东)》之铭言:
: 写了个矩阵相加相乘的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)