谁能帮我找出这段代码中的问题。我正在使用代码块 17.12。 我正在尝试制作一个 Matrix 类,我想在其中使用构造函数初始化矩阵,然后使用函数获取数组的成员。 然后重载“*”运算符以将两个输入的矩阵相乘。然后重载 ostream 以将已经给定的矩阵显示为输入或产品(如“cout<< m<< endl;)。
#include <iostream>
using namespace std;
class Matrix
{
private:
//static int row; //don't work
//static const int row; //don't work
//constexpr int row; //don't work
int row;
int column;
//Here my moto is to make a matrix which takes input from the user and
create the matrix of desired size at runtime.
double A[row][column];
public:
Matrix(int row,int column);
Matrix(Matrix &mat);
void setRowXColumn(int row,int column);
void setColumn(int column);
void setMatrix(Matrix A);
};
int main()
{
//Here 3 and 2 are the rows and columns of the matrix m respectively.
Matrix m(3,2);
return 0;
}
Matrix::Matrix(int row=0,int column=0)
{
setRowXColumn(int row,int column); //error: expected primary-expression before 'int'|
//what primary-expression?
}
Matrix::Matrix(Matrix &mat)
{
row=mat.row;
column=mat.column;
}
void Matrix::setRowXColumn(int row,int column)
{
if(row<0)
this->row=0;
else
this->row=row;
if(column<0)
this->column=0;
else
this->column=column;
}
//And i also want the members as input by the user at runtime.
void Matrix::setMatrix(Matrix A)
{
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
cout<<"Enter"<<Matrix A<<"["<<i<<"]"<<"["<<j<<"]"<<endl;
cin>>A[i][j];
}
}
}
从上面的代码我得到以下错误。
||=== 构建:在类矩阵中调试(编译器:GNU GCC 编译器)===|
Class Matrix\main.cpp|9|错误:无效使用非静态数据成员“Matrix::row”|
Class Matrix\main.cpp|7|注意:在这里声明|
Class Matrix\main.cpp|9|错误:无效使用非静态数据成员“Matrix::column”|
Class Matrix\main.cpp|8|注意:在这里声明|
Class Matrix\main.cpp||在构造函数'Matrix::Matrix(int, int)'中:|
Class Matrix\main.cpp|42|错误:“int”之前应为主表达式|
Class Matrix\main.cpp|42|错误:“int”之前应为主表达式|
类Matrix\main.cpp||成员函数'void Matrix::setMatrix(Matrix)':|
Class Matrix\main.cpp|69|错误:“A”之前应为主表达式|
Class Matrix\main.cpp|70|错误:“operator[]”不匹配(操作数类型为“Matrix”和“int”)|
||=== 构建失败:6 个错误,0 个警告(0 分钟,0 秒)===|
非常感谢您的帮助和感谢。 我是一名学生,目前正在学习 C++。 我仍在研究这段代码。
编辑:-到目前为止,我已经减少了错误,但是“双 A[行][列] 是我最头疼的问题。我想要这样,因为我想创建一个矩阵,就像我在主函数中所做的那样. 然后接下来将数组的成员作为输入。 希望这次修改能进一步澄清我的问题。
谢谢...
最佳答案
void Marix::setRowXColumn(int row,int column)
。它应该矩阵。如果您使用的是 IDE,它应该会警告您这些拼写错误。setRowXColumn(int row,int column)
应该是setRowXColumn(row,column);
c++ 语句总是需要一个“;”在最后。
double A[row][column];
如果你想创建一个“动态数组”,就这样做double **A;
.和A = new double*[row]; for(int i = 0; i < row; i++){ A[i] = new double[column]; }
在你的构造函数中,然后在你的解构函数中删除它。
我认为在这种情况下您可以只使用 vector 而不是数组。
关于c++ - |9|错误 : invalid use of non-static data member 'Matrix::row' |9|error: array bound is not an integer constant before ']' token|,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54956322/