을주고있다 :과부하 운영자는 나에게 오류
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
실제'Matrix' 객체 대신'array' 필드를 추가하려고하는 이유는 무엇입니까? –
적절한 연산자 오버로딩은 'm1 = m2 + m3'처럼 보일 것입니다. – Chad
이 라인의 의도는 무엇입니까 m1.array = 0; 그것은 컴파일합니까? – neagoegab