sách gpt4 ai đã đi

Chương trình C++ để cộng và nhân 2 ma trận

In lại 作者:行者123 更新时间:2023-11-28 02:56:46 28 4
mua khóa gpt4 Nike

我用 C++ 编写了这段代码,使用运算符重载对 2 个矩阵进行加法和乘法运算。当我执行代码时,它会在第 57 行和第 59 行产生错误,非法结构操作(两行都出现相同的错误)。请解释我的错误。提前致谢:)

class matrix{
công cộng:
int i, j, row, col, mat[10][10];
void input();
matrix operator+(matrix mm2);
matrix operator*(matrix mm2);
void output();
};
void matrix::input(){
cout<<"\nEnter number of rows and columns : ";
cin>>row>>col;
cout<<"\nEnter the elements : ";
for(i=0; i
for(j=0;j<>
cin>>mat[i][j];
}
}
}
matrix matrix::operator+(matrix mm2){
matrix temp;
temp.row=row;
temp.col=col;
for(i=0; i
for(j=0; j
temp.mat[i][j]=mat[i][j]+mm2.mat[i][j];
}
}
return temp;
}
matrix matrix::operator*(matrix mm2){
matrix temp;
temp.row=row;
temp.col=col;
for(i=0; i
temp.mat[i][j]=0;
for(j=0; j
temp.mat[i][j]+=mat[i][j]*mm2.mat[i][j];
}
}
return temp;
}
void matrix::output(){
cout<<"Matrix is : ";
for(i=0;i<>
for(j=0;j<>
cout<<>
}
cout<<"\n";
}
}
int main(){
matrix m1, m2, m3;
clrscr();
m1.input();
m2.input();
m3=m1+m2;
cout<<"\nSum is "<<>
m3=m1*m2;
cout<<"\nProduct is "<<>
getch();
trả về 0;
}

1 Câu trả lời

output() 是一个函数,不能直接放在 cout 流上。只需在单独的行中调用它即可。

改变

cout<<"\nSum is "<<>
...
cout<<"\nProduct is "<<>

đến

cout<<"\nSum is ";
m3.output();
...
cout<<"\nProduct is ";
m3.output();

或者,您可以为矩阵类重载 << 运算符。然后你可以做

cout<<"\nSum is "<<>
...
cout<<"\nProduct is "<<>

关于2个矩阵相加和相乘的C++程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21794803/

28 4 0
Bài viết được đề xuất: c++ - C++的delete和delete[]中的逻辑
Bài viết được đề xuất: C++ 均值和中值绝对偏差返回相同的奇怪符号
Bài viết được đề xuất: javascript - onBeforeUnload 和多种形式
Bài viết được đề xuất: C++ 使用 *this 从静态方法调用抽象方法
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com