2012-11-27 2 views
0

을주고있다 :과부하 운영자는 나에게 오류

m3.array = m1.array+m2.array; 

곳 m3, M1 및 M2를 int와 클래스 타입의 매트릭스의 모든 객체가 있습니다 [3] [3] array - int [3] [3] int [3] [3]과 opperand '+'의 호환되지 않는 지정을 다루는 오류가 계속 발생합니다. 내가 정확한 오류를 모르겠다. 왜냐하면 나는 컴퓨터에서 프로그램을 컴파일하지 않기 때문이다.

#include <iostream> 
#include <string> 
#include "matrix.h" 

using namespace std; 

Matrix::Matrix() 
{ 
    m1.array = 0; 
} 

istream& opeator >>(istream& inp, Matrix& m1) 
{ 
     int i, j; 

     for (i = 0; i < 3;i++) 
     { 
      for (j=0; j < 3;j++) 
      { 
       inp >> m1.array[i][j]; 
      } 
     } 
     return inp; 
} 
ostream& operator <<(istream& outp, Matrix& m1) 
{ 
     int i, j; 
     for (i = 0;i<3;i++) 
     { 
      for (j = 0;j<3;j++) 
      { 
       out<<m1.array[i][j]<<" "<<endl; 
      } 
     } 
     return outp; 
} 

Matrix operator + (const Matrix& m1, const Matrix& m2) 
{ 
     Matrix answer; 
     int i,j; 
     for (i = 0;i<3;i++) 
     { 
      for (j = 0;j<3;j++) 
      { 
       answer.array[i][j] = m1.array[i][j] + m2.array[i][j]; 
      } 
     } 
     return answer; 
} 

Matrix operator - (const Matrix& m1, const Matrix& m2) 
{ 
     Matrix answer; 
     int i, j; 
     for (i = 0;i<3;i++) 
     { 
      for (j = 0;j<3;j++) 
      { 
       answer.array[i][j] = m1.array[i][j] - m2.array[i][j]; 
      } 
     } 
     return answer; 
} 

Matrix operator * (const Matrix& m1, const matrix& m2) 
{ 
     Matrix answer; 
     int i, j, k; 
     for (i = 0;i<3;i++) 
     { 
      for (j = 0; j<3;j++) 
      { 
       for (k = 0; k<3;k++) 
       { 
        answer.array[i][j] = m1.array[i][k] + m2.array[k][j]; 
       } 
      } 
     } 
     return answer; 
} 

과 matrix.h :

#ifndef MATRIX_H 
#define MATRIX_H 

using namespace std; 

class Matrix 
{ 
     public: 
      Matrix(); 
      friend istream& operator >>(istream&, Matrix&); 
      friend ostream& operator <<(ostream&, const Matrix&); 
      friend Matrix& operator +(const Matrix&, const Matrix&); 
      friend Matrix& operator -(const Matrix&, const Matrix&); 
      friend Matrix& operator *(const Matrix&, const Matrix&); 
      int array[3][3]; 

}; 




#endif 
+0

실제'Matrix' 객체 대신'array' 필드를 추가하려고하는 이유는 무엇입니까? –

+2

적절한 연산자 오버로딩은 'm1 = m2 + m3'처럼 보일 것입니다. – Chad

+1

이 라인의 의도는 무엇입니까 m1.array = 0; 그것은 컴파일합니까? – neagoegab

답변

3
Matrix operator + (const Matrix& m1, const Matrix& m2) 

이 컴퓨터가 어떻게이 Matrix 객체, 좋은 일을 추가 할 수 알려줍니다.

m3.array = m1.array+m2.array; 

m1m2Matrix 객체하지만 m1.array는 없습니다. 해당int[3][3] 개체입니다. 다행히도 픽스는 매우 간단합니다 :

m3 = m1 + m2; 
1

멤버 array 유형 int[3][3]이다

다음은 내가 가진 matrix.cpp입니다. C++에서 의미가없는 두 개의 다차원 배열을 추가하려고합니다. 당신의 오버로드 된 연산자를 부를 것이다

m3 = m1 + m2; 

:

나는 당신이 정말로 원하는 것은 가정합니다.

또 다른 의심스러운 점은 친구 선언과 실제 정의가 일치하지 않는다는 것입니다. 반환 유형이 다릅니다.