c++ - |9|错误 : invalid use of non-static data member 'Matrix::row' |9|error: array bound is not an integer constant before ']' token|

标签 c++ matrix operator-overloading matrix-multiplication ostream

谁能帮我找出这段代码中的问题。我正在使用代码块 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[行][列] 是我最头疼的问题。我想要这样,因为我想创建一个矩阵,就像我在主函数中所做的那样. 然后接下来将数组的成员作为输入。 希望这次修改能进一步澄清我的问题。

谢谢...

最佳答案

  1. void Marix::setRowXColumn(int row,int column) 。它应该矩阵。如果您使用的是 IDE,它应该会警告您这些拼写错误。

  2. setRowXColumn(int row,int column) 应该是setRowXColumn(row,column);

  3. c++ 语句总是需要一个“;”在最后。

  4. 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/

相关文章:

c++ - 运算符重载函数的参数什么时候应该通过常量引用传递?

Linux Code::Blocks、EMACS 或 GVIM 上的 C++ 开发

c++ - 字符串大小始终为 32 字节

c++ - 是否允许编译器在重载解析期间选择 const ref 而不是 ref?

algorithm - 程序集处理三角矩阵存储器的算法

matrix - 如何从 Julia 中的对角矩阵中提取对角元素数组?

matlab - 根据每个元素在矩阵中出现的频率创建权重矩阵的最简单方法是什么?

c# - 重写相等运算符

c++ - Purify 在类/结构填充上的 Uninit 内存读取 (UMR)

c++ - 类模板中的重载运算符> friend