在C ++中打印出矩阵，类方法问题

``````#include <iostream>
#include <string>
#include <cmath>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
using namespace std;

class Matrix
{
public:
Matrix();
Matrix(int rowSize, int colSize);
void setSize(int rowSize, int colSize);
string printMatrix(int rowSize, int colSize);
void free_data();
void allocate_data();
private:
int rowSize;
int colSize;
double **p;
//vector<vector<int>> matrix(rowSize, vector<int>(colSize));

};
void Matrix::allocate_data()
{
p=new double *[rowSize];
for (int i=0; i<rowSize; i++)
{
p[i]= new double[colSize];
}
}
void Matrix::free_data()
{
for (int i = 0; i<rowSize; i++)
{
delete[] p[i];
}
delete[] p;
}

void Matrix::setSize(int rowSize, int colSize)
{
int newSize = 0;
p[rowSize][colSize]=newSize;

}

Matrix::Matrix(int rowSize, int colSize)
{
this->rowSize=rowSize;
this->colSize=colSize;
allocate_data();
for(int i=0; i<rowSize; i++)
{
for(int j=0; j<colSize; j++)
{
p[i][j]=0;
}
}
}

string Matrix::printMatrix(int rowSize, int colSize)
{
int i,j;
int matrix[i][j];
for ( int i = 0 ; i < rowSize ; i++ ) //it worked in main, but doesnt in class
{
for (int j=0; j<colSize; j++)
{
0>>matrix[i][j];
}
cout<<endl;
}
cout<<"Matrix"<<endl;
for (int i=0; i<rowSize; i++)
{
for(int j=0; j < colSize; j++)
{
cout<<matrix[i][j] << " ";
}
cout<<"\n";
}
cout<<endl;
}

int main()
{
int rowSize;
int colSize;
cout<<"Enter size of rows and columns: ";
cout<<"Rows: ";
cin>>rowSize;
cout<<"Cols: ";
cin>>colSize;

Matrix m;

m.printMatrix(rowSize,colSize);

return 0;
}
``````

• 冉天心 回复

您声明两个构造函数：

``````    Matrix();
Matrix(int rowSize, int colSize);
``````

但是您只定义了一个：

``````Matrix::Matrix(int rowSize, int colSize)
{
this->rowSize=rowSize;
this->colSize=colSize;
allocate_data();
for(int i=0; i<rowSize; i++)
{
for(int j=0; j<colSize; j++)
{
p[i][j]=0;
}
}
}
``````

Furthermore, in `main` you call the constructor that you happened to not define:

``````Matrix m;
``````

Instead, get rid of the declaration for `Matrix::Matrix()` since you lack a definition, and use the constructor that takes two arguments:

``````int main()
{
int rowSize;
int colSize;
cout<<"Enter size of rows and columns: ";
cout<<"Rows: ";
cin>>rowSize;
cout<<"Cols: ";
cin>>colSize;

Matrix m(rowSize, colSize);

m.printMatrix(rowSize,colSize);

return 0;
}
``````

其他改进

You don't need to pass the size of the matrix to `Matrix::printMatrix`, since the matrix already has the size as members. By passing those parameters to `printMatrix, you're making the class more confusion and error-prone to use. If I constructed a 3x3 matrix and tried to call`printMatrix(10,10)` on it, I would certainly invoke undefined behavior and likely crash the program.

Get rid of the parameters (in both definition and declaration) and use the existing `rowSize` and `columnSize` fields to control the loops in that function.

Also, `0 >> matrix[i][j];` makes no sense. The correct syntax is `matrix[i][j] = 0;`/

• 原静安 回复

您定义并使用无参数的Matrix构造函数（Matrix（）;），但没有实现它。

您可以：

• Delete the Matrix() definition and use the constructor that you define (Matrix m(rowSize, colSize);)

• Implement the parameterless constructor, creating the matrix with some predefined values

``````Matrix::Matrix() {
this(4, 4);
}
``````

我建议第一个选择