| 9 |错误:无效使用非静态数据成员'Matrix :: row'| 9 |错误:数组绑定不是']'令牌之前的整数常量|

问题描述 投票:-1回答:1

有人可以帮我找出这段代码中的问题。我正在使用代码块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];
        }
    }
}

从上面的代码我得到以下错误。

|| === Build:Class Matrix中的Debug(编译器:GNU GCC编译器)=== |

Class Matrix \ main.cpp | 9 | error:无效使用非静态数据成员'Matrix :: row'|

Class Matrix \ main.cpp | 7 |注意:在此声明|

Class Matrix \ main.cpp | 9 | error:无效使用非静态数据成员'Matrix :: column'|

Class Matrix \ main.cpp | 8 |注意:在此声明|

Class Matrix \ main.cpp ||在构造函数'Matrix :: Matrix(int,int)'中:|

Class Matrix \ main.cpp | 42 | error:在'int'|之前预期的primary-expression

Class Matrix \ main.cpp | 42 | error:在'int'|之前预期的primary-expression

Class Matrix \ main.cpp ||在成员函数'void Matrix :: setMatrix(Matrix)'中:|

Class Matrix \ main.cpp | 69 | error:在'A'|之前的预期primary-expression

Class Matrix \ main.cpp | 70 | error:'operator []'不匹配(操作数类型为'Matrix'和'int')|

|| ===构建失败:6个错误,0个警告(0分钟,0秒(秒))=== |

我非常感谢您的帮助,谢谢。我是一名学生,目前正在学习c ++。我还在研究这段代码。

编辑: - 到目前为止,我已经减少了错误,但“双A [行] [列]是我头疼的主要问题。我想要这样,因为我想创建一个像我在main函数中所做的那样的矩阵。然后接下来把数组的成员作为输入。希望这个编辑进一步澄清我的问题。

谢谢...

c++ matrix operator-overloading matrix-multiplication ostream
1个回答
1
投票
  1. void Marix::setRowXColumn(int row,int column)。它应该矩阵。你使用IDE,它应该警告你那些错别字。
  2. setRowXColumn(int row,int column)应该besetRowXColumn(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而不是array。
© www.soinside.com 2019 - 2024. All rights reserved.