在类矩阵mat(m,n)内初始化vector > data(m,vector (n))

时间:2022-09-24 23:20:49

I have to following code:

我必须遵循以下代码:

class matrix{
int m,n;
vector<vector<int>> dat(m,vector<int>n);
matrix(int m, int n){this->m=m;this->n=n;}
}

In order to initialize dat, we must already have m and n. I doubt that this would be happening. The error I'm getting is

为了初始化dat,我们必须已经有m和n。我怀疑这会发生。我得到的错误是

'm' is not a type

'm'不是一种类型

I cannot seem to figure out how to work around this.

我似乎无法弄清楚如何解决这个问题。

1 个解决方案

#1


2  

Use the member initializer list:

使用成员初始化列表:

class matrix
{
    int m,n;
    vector<vector<int>> dat;

public:
    matrix(int m, int n) : m (m), n(n), dat(m, vector<int>(n))
    {
    }
};

#1


2  

Use the member initializer list:

使用成员初始化列表:

class matrix
{
    int m,n;
    vector<vector<int>> dat;

public:
    matrix(int m, int n) : m (m), n(n), dat(m, vector<int>(n))
    {
    }
};