나는 연산자 오버로딩을 사용하여 행렬을 더하는 프로그램을 만들었습니다. 프로그램을 만들었지 만 행렬 B에 대한 입력을받지 않습니다. 프로그램과 도움을보십시오. 이 행렬 입력은 noramlly 두 개의 다른 객체로 메인에서 가져온 다음, '+'연산자를 오버로드하여 2 개의 객체 멤버를 추가하고 '< <'연산자가 오버로드되었음을 나타내는 열도 사용했습니다.연산자 오버로딩을 사용한 행렬 덧셈
#include<iostream>
using namespace std;
int s;
class matrix
{
public:
int **m1,**m;
friend matrix operator +(matrix &t,matrix &t1);
friend ostream& operator << (ostream &out,matrix &t);
};
matrix operator +(matrix &t,matrix &t1)
{
for(int x=0;x<s;x++)
{
for(int y=0;y<s;y++)
{
t.m1[x][y]+=t1.m1[x][y];
}
}
}
ostream& operator << (ostream &out,matrix &t2)
{
for(int p=0;p<s;p++)
{
for(int q=0;q<s;q++)
{
out<<t2.m[p][q]<<" ";
}
out<<endl;
}
}
int main()
{
cout<<"Enter the size of matrices(SQUARE MAT ONLY):: ";
cin>>s;
matrix t,t1,t2;
for(int i=0;i<s;i++)
{
for(int j=0;j<s;j++)
{
cout<<"Enter value ("<<i+1<<","<<j+1<<") for matrix A::";
cin>>t.m1[i][j];
}
}
for(int i=0;i<s;i++)
{
for(int j=0;j<s;j++)
{
cout<<"Enter value ("<<i+1<<","<<j+1<<") for matrix B::";
cin>>t1.m1[i][j];
}
}
t2=t+t1;
cout<<t2;
}
매트릭스 구성 요소의 내부 표현을위한 메모리는 어디에서 구합니까? – Jodocus
왜'std :: vector'를 사용하지 않습니까? '**'는 아스키 예술에 좋지만, 좋은 코드가 아닙니다. – user463035818
또는 표준 행렬 대수 라이브러리. [Eigen] (예 : http://eigen.tuxfamily.org/index.php?title=Main_Page). – Thomas