Declaring arrays in the private part of a class

I have a class, and part of the input to the class is a vector (called Data) of variable length (say, it has length N). I included this after the function:

    N = data_->size();

In the private section of the class, I want to declare an array double A[N][N];. However, when I try to do this, I get something saying

error: "N is not a type name, static, or enumerator."

How to create an array A[N][N]?

Sorry if this has already been explained elsewhere, as I am very new to C ++, so I donโ€™t even know what to look for!

Edit - nested code:

    class foo {     
    public:
        foo (std::vector &data)
    : data(data_)
    {
        N = data_->size();
        M =  /* four times the last member of data (which is a vector of positive integers)*/
    }

    private:
      double A[M][M];

    void foo(void)
    {
      for (std::size_t i=1; i<=M; ++i)
        {
          A[i][i] = 1;
        }
    }
    };

Hope this makes some sense ... How could I define A [M] [M]? Perhaps this cannot be done for M, since M is a data function. If this is not possible for M, is it possible that N?

, , A a std::vector< std::vector<double> > A, 0 - , THEN ...

+5
4

... ...

  • N, M ( , foo)
  • data, ( data_(data), member(expression), expression(member))
  • : std::vector< sometype >; , , boost
  • -, (A)
  • void foo(void) - , ( ),

- ,

foo:

class foo {
};

, std::vector<double>

class foo {
public:
  foo(std::vector<double> &data);
};

-

class foo {
private:
  std::vector<double> data_;
public:
  foo(std::vector<double> &data)
    :data_(data)
  {};
};

, , , , , - . .

class foo {
private:
  std::vector<double> data_;
  size_t N;
 public:
  foo(std::vector<double> &data)
    :data_(data)
    ,N(data.size())
  {};

};

, , , , . Kerrek SB, :

class foo {
private:
  std::vector<double> data_;
  size_t N;
  std::vector< std::vector<double> > A;
public:
  foo(std::vector<double> &data)
    :data_(data)
    ,N(data.size())
    ,A()
  {
    A.resize(N);
    for (size_t i=0; i<N; ++i) {
      A[i].resize(N);
    }
  };
};
+1

std::vector, data_ (, , ), :

A = vector<vector<double> >(N, vector<double>(N, 0));

, - .

, , .

+5

. , . . , . , , , std::vector. , , , , N * N, j + N * i.

:

std::vector< std::vector<int> > v(N, std::vector<int>(N));

:

std::vector< std::vector<int> > v;
//...
v.resize(N, std::vector<int>(N));

: v[2][4] = 8;


:. , - , N * 4n, data.back() == n:

std::vector<unsigned int> data = get_data(); // given!

std::vector< std::vector<double> > v(data.size(), std::vector<double>(4 * data.back()));
+4

, , .

double **A;

:

A = new double *[N];
for(i=0;i<N;++i)
{
  A[i] = new double [N];
}

, delete , , ... : . .

0

All Articles