在类的私有部分声明数组

发布于 2024-12-07 13:17:47 字数 962 浏览 0 评论 0原文

我有一个类,该类的部分输入是一个可变长度的向量(称为数据)(假设它的长度为 N)。我已将其包含在函数之后:

    N = data_->size();

在类的私有部分中,我想声明一个数组 double A[N][N];。然而,当我尝试这样做时,我听到一些话

错误:“N 不是类型名称、静态或枚举数”。

如何创建数组 A[N][N]

抱歉,如果这已经在其他地方解释过,因为我对 C++ 很陌生,所以甚至不知道要寻找什么!

编辑 - 附加代码:

    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;
        }
    }
    };

希望这有某种意义......我如何能够定义 A[M][M]?也许对 M 来说这是不可能的,因为 M 是数据的函数。如果M不可能,N也可能吗?

我能想到的一种可能性是我可以将 A 设为 std::vector< std::vector> A 然后将很多 0 或其他内容推入其中,然后修改值...

I've got a class, and part of the input into the class is a vector (called Data) of variable length (lets say it has length N). I've 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 do I create the array A[N][N]?

Sorry if this is already explained somewhere else, as I'm very new to c++, so wouldn't even know what to look for!

Edit -- attached 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 that makes some sort of sense... How would I be able to define A[M][M]? Maybe it's not possible to do it for M as M is a function of the data. If not possible for M, is it possible for N?

One possibility I can think of is that I can make A a std::vector< std::vector<double> > A and then push a lot of 0's or something into it, and THEN modify the values...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尹雨沫 2024-12-14 13:17:47

如果您使用 std::vector 类,则必须在 data_ 类的函数(例如构造函数)中创建向量,使用以下句子:

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

括号的第一个参数是向量的大小,第二是它的数据类型。

抱歉我的英语,我是西班牙人,而且我的英语不是很好。

if you´re using the std::vector class you must creates the vector in a function of the data_ class (like the constructor, for example), using this sentence:

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

The first parameter of the parentheses is the size of the vector and the second is the type of data on it.

Sorry for my english, i´m spanish and my english isn´t very good.

一腔孤↑勇 2024-12-14 13:17:47

你不能那样做。数组是类型,必须在编译时知道它们。这包括它们的尺寸。所以你不能将动态数组作为类的一部分。您得到的最接近的东西是指向手动分配的数组的指针,但实际上它本质上是一个 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()));

You cannot do that. Arrays are types, and they have to be known at compile time. This includes their sizes. So you cannot have a dynamic array as part of your class. The closest thing you get is a pointer to manually allocated array, but that is in fact essentially a std::vector. So perhaps the easiest solution is to just have a vector of vectors, or perhaps a single vector of size N * N that you access in strides j + N * i.

Example:

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

Or:

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

Access: v[2][4] = 8;


Update: Since you edited your answer, you can write something like this to get you an N * 4n vector, where 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()));
完美的未来在梦里 2024-12-14 13:17:47

嗯...这里有几个问题...

  • NM 均未声明(在构造函数中或作为 foo 的成员
  • )尝试初始化未声明的成员data(您可能指的是data_(data),因为语法是member(expression),而不是表达式(member))
  • 构造函数的参数是不完整的类型:它需要是 std::vector<某种类型>;如果你希望它是通用的,你需要使用模板或从 boost 获得一些帮助
  • 你没有初始化你声明的唯一成员变量 (A)
  • void foo(void) 是一个问题,因为它不是构造函数(没有类型)的正确语法,而是使用类名

让我们构建更接近您想要的内容

从类 foo 开始:

class foo {
};

构造函数采用单个类型参数std::vector

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);
    }
  };
};

Hmmm...several issues here...

  • Neither N nor M are declared (either in the constructor or as members of foo)
  • You are trying to initialize a member data that is not declared (and you may mean data_(data) as the syntax is member(expression), not expression(member))
  • The argument to your constructor is an incomplete type: it needs to be std::vector< sometype >; if you want it to be generic you'll need to use templates or get some help from boost
  • You are not initializing the only member variable that you have declared (A)
  • void foo(void) is a problem because it is not the correct syntax for a constructor (which has no type) but uses the class name

Let's build up to something closer to what you want

Start with a class foo:

class foo {
};

with a constructor taking a single argument of type std::vector<double>

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

you want to initialize a member variable with the data

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

At this point I'll note that I would generally not put the definition of a non-trivial constructor in the class declaration, but in a implementation file instead, and consequently I would be able to put the declaration of member variable beneath the public section with the declaration of the constructor. But for compactness I'll leave this way here.

You want to capture and store the size of the data

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

};

At this point we still haven't made that multi-dimensional storage that you want, but now you have some decisions to make about how to manage the storage. If you use Kerrek SB's approach this looks something like

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);
    }
  };
};
哑剧 2024-12-14 13:17:47

如果您不想自己管理内存,则需要使用智能指针动态创建它。

double **A;

在构造函数中:

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

并不是说您必须在析构函数中调用delete,现在要遵守三规则,您必须有一个复制构造函数和一个赋值运算符...这里使用智能指针更好:请参阅 Boost 智能指针帮助页面。

You need to create it dynamically, using a smart pointer if you do not want to manage memory yourself.

double **A;

in the constructor:

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

Not that you have to call delete in your destructor and now to obey the rule of three you have to have a copy constructor and a assignment operator... Using a smart pointer is better here: see the Boost smart pointer help page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文