【发布时间】:2015-12-21 01:00:08
【问题描述】:
我在下面包含了对我的 C++ 程序的描述,但我觉得这个问题可能太基本了,所以没有必要描述。本质上,我的“parse_matrix”方法出现了段错误。我已经进行了一些调试并标记了运行结束的行,但我无法弄清楚到底出了什么问题。我正在尝试做的是使用 cin 从输入文件中获取一个数字并将其放入索引 [i][j] 处的矩阵(实现为向量
这是程序说明:
我正在编写一个程序,该程序基本上将获取一个包含矩阵信息和要执行的操作的输入文件,并将输出结果矩阵。输入文件的第一行会说“加法”或“乘法”,第二行会说有多少矩阵。然后,每个矩阵将有两行,第一行有两个数字,由空格分隔,确定后续矩阵的维度(行列),其内容将写在一行中并分组为行(所以一个 2x2 矩阵将是 A(1,1) A(1,2) A(2,1) A(2,2) (1,1 是左上角,1,2 是右上角,2,1 是左下角,2,2 是右下角)。
一个示例输入文件,它告诉程序添加 3 个矩阵,所有维度均为 2x2:
add
3
2 2
1 2 3 4
2 2
1 1 1 1
2 2
1 0 1 0
这是我的代码(我已经标记了问题似乎发生的位置)。任何帮助将不胜感激!
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
using namespace std;
//define dot product
int dot(vector<int> a, vector<int> b){
int c=0;
int size = a.size();
for (int i=0; i<size; i++){
c += a[i]*b[i];
}
return c;
}
//define Matrix struct
struct Matrix {
vector<vector<int>> mtrx;
int m;
int n;
//overloading "+" for adding matrices
Matrix operator+ (const Matrix& b){
Matrix c;
for (int i=0; i<b.m; i++){
for (int j=0; j<b.n; j++){
c.mtrx[i][j] = this->mtrx[i][j] + b.mtrx[i][j];
}
}
return c;
}
//overloading "*" for multiplying matrices
Matrix operator* (const Matrix& b){
Matrix c;
for (int i=0; i<b.m; i++){
for (int j=0; j<b.n; j++){
vector<int> temp;
for (int k=0; k<b.m; k++){
temp.push_back(b.mtrx[k][j]);
}
c.mtrx[i][j] = dot(this->mtrx[i],temp);
}
}
c.n = b.n;
return c;
}
};
//will apply the designated operation to two matrices
Matrix operation(Matrix a, Matrix b, string op_type){
Matrix c;
if (op_type=="add"){
if (a.m == b.m && a.n == b.n) {
c = a+b;
}
else {
cout<<"Dimensions do not match. Addition cannot be performed.";
exit(1);
}
}
else if (op_type == "multiply"){
if (a.n == b.m){
c = a*b;
}
else {
cout<<"Inner dimensions do not match. Multiplication cannot be performed.";
exit(1);
}
}
else {
cout<<"This program does not "<<op_type<<" matrices. It only adds or multiplies.";
exit(1);
}
return c;
}
//reads line and sets matrix elements
void parse_matrix(Matrix *p){
int m,n;
cin>>m>>n;
p->m = m;
p->n = n;
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++){
int x;
cin>>x;
/************THIS IS WHERE THE SEGFAULT OCCURS***************/
((p->mtrx)[i]).push_back(x);
}
}
}
//prints matrix elements in a line (row, column) lexicographical ordering using cout
void print_matrix(Matrix matrix){
int m = matrix.m;
int n = matrix.n;
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++){
cout<<matrix.mtrx[i][j];
}
cout<<endl;
}
}
//main will read in type of operation, number of matrices, and individual matrices
//each matrix resides on a line, and will be parsed into rows and columns
//each matrix line will be preceded by a line of two ints that specifies the matrix dimensions
int main() {
string op_type;
cin>>op_type;
int num;
cin>>num;
Matrix matrix;
Matrix final;
parse_matrix(&final);
for (int k=1; k<num; k++) {
parse_matrix(&matrix);
operation(final, matrix, op_type);
}
print_matrix(final);
return 0;
}
【问题讨论】:
-
temp中的parse_matrix是什么?据我所知,它是一个未初始化的变量,代码不可能出现段错误,因为它甚至不编译,更不用说运行了。 -
这段代码有很多问题。您可以通过正确初始化 Matrix 变量来开始修复它们。你现在拥有它的方式是向量是空的,当你尝试访问它们时你会崩溃。
标签: c++ matrix vector segmentation-fault