c++之学习程序Matrix_1

来源:互联网 发布:思迅商云网络断开 编辑:程序博客网 时间:2024/05/21 06:55

  还是关于重载运算的程序,是对关于矩阵相加的"+"重载;不知道为什么有个问题还是出了点问题,同学来借用电脑,只好先放着,以后再思考吧。

#include<iostream>
using namespace std;
#define N 2
#define M 3
class Matrix
{
public:
 Matrix();
    friend Matrix operator+(Matrix &,Matrix &);
 void input();
 void display();
private:
 int mat[N][M];
};
Matrix::Matrix()
{
 for(int i=0;i<N;i++)
  for(int j=0;j<M;j++)
   mat[i][j]=0;
}
Matrix  operator+(Matrix &a,Matrix &b)
{
 Matrix c;
 for(int i=0;i<N;i++)
  for(int j=0;j<M;j++)
   c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
  return c;
}
void Matrix::input()
{
 cout<<"input value of the Matrix [N][M]"<<endl;
 for(i=0;i<N;i++)
  for(j=0;j<M;j++)
   cin>>mat[i][j];
}
void Matrix::display()
{
 for(i=0;i<n;i++)
 {
     for(j=0;j<m;j++)
   cout<<mat[i][j]<<" ";
  cout<<endl;
 }
}
int main()
{
 Matrix a,b,c;
 a.input();
 b.input();
 c=a+b;
 c.display();
 return 0;

}

错误提示:
C:/Program Files/Microsoft Visual Studio/MyProjects/Matrix/Matrix.cpp(9) : fatal error C1001: INTERNAL COMPILER ERROR
        (compiler file 'msc1.cpp', line 1786)
         Please choose the Technical Support command on the Visual C++
         Help menu, or open the Technical Support help file for more information
Error executing cl.exe.

Matrix.obj - 1 error(s), 0 warning(s)

原创粉丝点击