C++成员是结构的类:无法理解编译器错误

发布于 12-07 02:50 字数 684 浏览 3 评论 0原文

我想创建一个类,其 private: 成员之一是 struct point(见下文)。公共成员 ndimnumarticles 是 由用户在运行时设置,用于在类中创建相应的数组。但是我收到编译器错误。我不明白我哪里搞砸了。

显示的编译器错误:

nbdsearch.h:25: error: ‘point’ does not name a type
nbdsearch.h:24: error: invalid use of non-static data member ‘nbdsearch::ndim’
nbdsearch.h:31: error: from this location
nbdsearch.h:31: error: array bound is not an integer constant before ‘]’ token

类代码:

class nbdsearch{
 public: 
  int ndim,numparticles;
  point particlevec[numparticles];


 private: 
  struct point{
    string key;
    double cood[ndim];
              };

};

I want to create a class one of whose private: members is a struct point(see below). The public members ndim and numparticles are
set at run-time by the user, which are used to create corresponding arrays inside the class . However I am getting a compiler error. I don't understand where I messed up.

The compiler error being displayed:

nbdsearch.h:25: error: ‘point’ does not name a type
nbdsearch.h:24: error: invalid use of non-static data member ‘nbdsearch::ndim’
nbdsearch.h:31: error: from this location
nbdsearch.h:31: error: array bound is not an integer constant before ‘]’ token

The class code:

class nbdsearch{
 public: 
  int ndim,numparticles;
  point particlevec[numparticles];


 private: 
  struct point{
    string key;
    double cood[ndim];
              };

};

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

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

发布评论

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

评论(3

衣神在巴黎2024-12-14 02:50:54

nbdsearch.h:31: 错误:数组边界不是“]”标记之前的整数常量

double cood[ndim];

数组大小需要是编译时常量,而 ndim 显然不是。 ndim 是一个变量。

错误:“点”未命名类型

point particlevec[numparticles];

在第 25 行,编译器不知道 point 是什么。该结构稍后定义。 C++ 中的编译器采用自上而下的方法工作(不确定 C++0X 是否放宽了此规则)。因此,无论使用什么类型,都应该事先知道。

试试这个 -

class nbdsearch{
 private: 
  struct point{
    string key;
    std::vector<double>cood;
  };
 public: 
    int ndim,numparticles;
    std::vector<point> particlevec;   
};

nbdsearch.h:31: error: array bound is not an integer constant before ‘]’ token

double cood[ndim];

Array size needs to be a compile time constant and ndim clearly isn't. ndim is a variable.

error: ‘point’ does not name a type

point particlevec[numparticles];

On line 25, compiler doesn't know what point is. The structure is defined at a later point. Compilers in C++ work on a top to bottom approach(Not sure whether C++0X relaxes this rule). So, what ever types being used should be known to it before hand.

Try this -

class nbdsearch{
 private: 
  struct point{
    string key;
    std::vector<double>cood;
  };
 public: 
    int ndim,numparticles;
    std::vector<point> particlevec;   
};
澉约2024-12-14 02:50:54

point 需要在使用前声明。尝试将 private: 块放在 public: 块之前。

point needs to be declared before its use. Try putting the private: block before the public: block.

愚人国度2024-12-14 02:50:54

您的代码存在一些不同的问题。

  1. point 的声明需要对编译器可见
    你使用它

  2. 你试图从不是编译时常量的变量创建数组; C++ 不允许创建可变长度数组。

要解决这些问题,请执行以下操作:

  1. point 的声明块移至您使用它的位置上方。请注意,由于 point 的定义是 private,因此调用 nbdsearch::articlevec 的人将无法存储该值。他们所能做的就是将其传递给 nbdsearch 的另一个成员函数(或 friend 函数)。

  2. particlevec 的声明更改为 std::vector;粒子向量。对 point::cood 进行类似的更改。一旦用户指定了 ndim 和 ndim 的值, numarticles 使用 std::vector::resize 适当调整数组大小。

There are a few different problems with your code.

  1. The declaration of point needs to be visible to compiler before
    you use it

  2. You're trying to create array from variables that are not compile time constants; C++ does not allow creation of variable length arrays.

To fix these problems:

  1. Move the declaration block for point above where you use it. Note that since the definition of point is private, someone calling nbdsearch::particlevec will not be able to store that value. All they could do is pass it along to another member function (or friend function) of nbdsearch.

  2. Change declaration of particlevec to std::vector<point> particlevec. Make a similar change for point::cood. Once the user specifies values for ndim & numparticles use std::vector::resize to size the arrays appropriately.

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