1
這裏是我的代碼的一部分,當我編譯它時,它說 1:沒有匹配運算符= 2:沒有已知的從'Matrix'到'Matrix &「 但如果我刪除操作+部分它的工作原理 哪裏出了問題? :|編譯錯誤:重載運算符不匹配
GCC錯誤: 「無匹配關於 '操作符=' 在 'Z =矩陣::運算+(基質&)((* & Y))' 候選是: ATRIX &矩陣::運算=(矩陣&) 沒有已知的轉換爲自變量1從 '矩陣' 到 '矩陣&「'
class Matrix {
//friend list:
friend istream& operator>>(istream& in, Matrix& m);
friend ostream& operator<<(ostream& in, Matrix& m);
int** a; //2D array pointer
int R, C; //num of rows and columns
static int s1, s2, s3, s4, s5;
public:
Matrix();
Matrix(const Matrix&);
~Matrix();
static void log();
Matrix operator+ (Matrix &M){
if(R == M.R && C == M.C){
s4++;
Matrix temp;
temp.R = R;
temp.C = C; temp.a = new int*[R];
for(int i=0; i<R; i++)
temp.a[i] = new int[C];
for(int i=0; i<R; i++)
for(int j=0; j<C; j++)
temp.a[i][j] = a[i][j] + M.a[i][j];
return temp;
}
}
Matrix& operator = (Matrix& M){
s5++;
if(a != NULL)
{
for(int i=0; i<R; i++)
delete [] a[i];
delete a;
a = NULL;
R = 0;
C = 0;
}
R = M.R;
C = M.C;
a = new int*[R];
for(int i=0; i<R; i++)
a[i] = new int[C];
for(int i=0; i<R; i++)
for(int j=0; j<C; j++)
a[i][j] = M.a[i][j];
return *this;
}
};
請包括_complete_和你的問題_unedited_錯誤信息,並指出他們對哪些行。 – 2013-03-17 09:32:35
並請顯示您的'類矩陣'聲明的相關部分。此外,編譯錯誤是不是裏面GCC,它是你的代碼裏面,所以我覺得標題是容易出錯... – 2013-03-17 09:33:27
您_do_有問題,很容易看到,那就是'運營商+'功能呢如果if語句爲false,則不返回任何內容。這可能會導致問題和未定義的行爲。 – 2013-03-17 09:35:31