[問題] compile 過不了???

看板C_and_CPP作者 (神奇)時間15年前 (2010/08/04 23:36), 編輯推噓1(109)
留言10則, 5人參與, 最新討論串1/1
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 ) ( 未必需要依照此格式,文章條理清楚即可 ) 開發平台: Dev-C++ 補充說明: class Matrix { public: int nRow; int nCol; Complex **z; Matrix(); Matrix(int x, int y); ~Matrix(); Matrix operator+(Matrix &m); void operator=(Matrix &m); }; Matrix Matrix::operator+(Matrix &m) { cout << "+" << endl; Matrix y(this->nRow, this->nCol); int i, j; for(i=1; i<=this->nRow; i++) for(j=1; j<=this->nCol; j++) y.z[i][j] = this->z[i][j]+m.z[i][j]; return y; } void Matrix::operator=(Matrix &m) { cout << "=" << endl; this->nRow = m.nRow; this->nCol = m.nCol; int i, j; for(i=1; i<=this->nRow; i++) for(j=1; j<=this->nCol; j++) this->z[i][j] = m.z[i][j]; //return *this; } int main(int argc, char *argv[]) { while(true) { Matrix x(3, 3); Matrix y(3, 3); Matrix z(3, 3); z = x+y; } system("PAUSE"); return EXIT_SUCCESS; } 編譯器在這行 y = x+y; 出現 no match for 'operator=' in 'z = Matrix::operator+(Matrix&)(((Matrix&)(&y)))' 但是單純打 x+y 或 y = x 都可以過 實在不知道為什麼 麻煩請大家指點一下 如果需要其他程式碼我在po上 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.231.212

08/04 23:38, , 1F
void Matrix::operator=(const Matrix &m)?
08/04 23:38, 1F

08/04 23:43, , 2F
R value不能轉成L reference
08/04 23:43, 2F

08/04 23:43, , 3F
要用1樓的作法
08/04 23:43, 3F

08/04 23:45, , 4F
R value不能轉成non-const L-ref 可以轉成const L-ref
08/04 23:45, 4F

08/04 23:48, , 5F
簡單來說, x + y 會產生一個暫時物件, 參考到暫時物件
08/04 23:48, 5F

08/04 23:48, , 6F
優秀 謝謝大家
08/04 23:48, 6F

08/04 23:48, , 7F
只能用 type const &
08/04 23:48, 7F

08/04 23:49, , 8F
等到C++0x 就有R-ref這種東西了XD
08/04 23:49, 8F

08/04 23:51, , 9F
想不到 C++0x | x = a-f 已經成真了 <囧>
08/04 23:51, 9F

08/05 01:29, , 10F
樓上 XDDDDD
08/05 01:29, 10F
文章代碼(AID): #1CMOaGxM (C_and_CPP)