打印出c++中的矩阵,类方法问题



我想使用类和方法显示矩阵。我正在获取"C:\Users\PC\AppData\Local\Temp\cc41EMS5.o:lab6.cpp:(.text+0x3d6(:对"矩阵::矩阵(("的未定义引用"collect2.exe:error:ld返回了1个退出状态"错误。我想这是因为printMatrix方法(主要不是一个方法(,但我不知道如何修复。这是代码:

#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
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 doesn't 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;
}
}
} 
nt main(({int rowSize;int colSize;cout<lt"输入行和列的大小:";;cout<lt"行:";cin>gt;rowSize;cout<lt"Cols:";;cin>gt;colSize;
Matrix m(rowSize, colSize);
m.printMatrix(rowSize,colSize);

返回0;}


## Additional improvements
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 confusing 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. The return type also ought to be `void`.
Also, `0 >> matrix[i][j];` makes no sense. The correct syntax is `matrix[i][j] = 0;`.

定义并使用无参数的矩阵构造函数(Matrix(((,但您没有实现它。

您可以:

  • 删除Matrix((定义并使用您定义的构造函数(Matrix m(rowSize,colSize((

  • 实现无参数构造函数,创建具有一些预定义值的矩阵。您可以使用委派构造函数来调用接受参数的构造函数

    Matrix::Matrix() : Matrix(4,4) {
    }
    

我推荐第一个选项

最新更新