使用结构添加两个矩阵
Addition of two matrix using struct
我正在尝试使用结构对两个矩阵求和,但它不起作用。
如果可以优化此代码,请告诉我:D
编译方式:
g++ -O2 -Wall program.cpp -o program
输出:
在/usr/include/c++/4.8/iostream:39:0 包含的文件中, 来自Proy3.cpp:2:/usr/include/c++/4.8/ostream:548:5:注意:模板 std::basic_ostream& std::operator<<(std::basic_ostream&, const unsigned char*) operator<<(basic_ostream& __out, const unsigned char* __s) ^/usr/include/c++/4.8/ostream:548:5: 注意:模板参数推导/替换失败:Proy3.cpp:51:30:注意:"std::istream {aka std::basic_istream}"不是从"std::basic_ostream"派生的 cin <<&M2.x[i][j];
法典:
# include < cstdio >
# include < iostream >
typedef struct Matrix
{
int row, column;
int x[20][20];
};
Matrix M1,M2;
使用命名空间标准;
int main(){
cout << "Insert size rows: Mat[a]";
cin >> M1.row);
cout << "Insert size of columns Mat[a]";
cin >> M1.column;
cout << "Insert size of rows Mat[b]";
cin >> M2.row;
cout << "Insert size of columns Mat[b]";
cin >> M2.column;
int i, j;
// Matrix x
for(i = 0; i <= M1.row; i++)
{
for(j = 0; j <= M1.column; j++)
{
cout << "Insert number for matrix X : n";
cin >> M1.x[i][j]
}
}
// Matrix y
for(i = 0; i <= M2.row; i++)
{
for(j = 0; j <= M2.column; j++)
{
cout << "Insert number for matrix Y : n";
cin << M2.x[i][j];
}
}
// Matrix X + Matrix Y
for(i = 0; i <= M1.row; i++)
{
for(j = 0; j < M1.column; j++)
{
cout <<"The sum of " << M1.x[i][j] << " + " << M2.x[i][j] << " = " << M1.x[i][j] + M2.x[i][j] << endl;
}
}
return 0;
}
for(i = 0; i <= M2.M1.row; i++)
{
for(j = 0; j <= M2.M1.column; j++)
{
cout << "Insert number for matrix Y : n";
cin << &M2.M1.y[i][j];
}
}
没有您尝试访问的元素M2.M1.y
。还有你为什么要在M2
内部宣布M1
.您只能有一个结构,并有两个实例。类似的东西
struct matrix
{
int row,column;
int X[20][20];
};
struct matrix M1,M2;
现在您可以输入两个矩阵。
此外,您必须使用cin>>a
而不是cin>>&a
。
也cin << &M2.x[i][j];
应该是
cin >> M2.x[i][j];
^^^^
我认为cin state ment应该是cin>> &M2.x[i][j];
而不是 cin <<&M2.x[i][j];
相关文章:
- 在两个结构之间共享数组
- C++ 如何从函数传递两个结构?
- 如何认为两个结构相等,而不管它们的第二个属性如何?
- 使用用户函数在 C++ 中交换两个结构
- 在CPP中互相访问的两个结构
- 如何实现 std::map::查找包含两个结构'Pos'结构的比较逻辑,每个结构包含 x 和 y 坐标
- 当我们在两个结构上使用它时,= 运算符会做什么
- 合并/连接两个结构
- C++(SDL2)中的碰撞函数.如何传递存储在不同向量中的两个结构
- 具有相同名称和不同模板参数的两个结构如何工作
- 复制两个结构指针时出现分段错误
- Xcode 不链接两个结构
- 在两个结构(函子)中理解typedef的作用域
- 如何在c++中比较两个结构字符串
- 设置两个结构体相等后,指针为NULL
- 如何声明具有other类型成员的两个结构体
- 如何在c++中连接两个结构体类型变量
- C++ stl 列出了两个结构交叉引用
- 有两个结构在C++中相互引用彼此的变量
- 为什么我的两个结构数组没有相互作用?