简体   繁体   English

C ++中的全局动态分配的结构

[英]Global dynamically allocated struct in c++

I'm having problems trying to use an array of structs which doesn't have an initial size. 我在尝试使用没有初始大小的结构数组时遇到问题。 How do I do this? 我该怎么做呢? This is my struct: 这是我的结构:

struct carbon {
    double temp;
    double mass;
    rowvec::fixed<3> position;      
    rowvec::fixed<3> velocity;
    rowvec::fixed<3> force;
} *atom;

During my program I'm allocating size of the struct array like this: 在我的程序期间,我正在分配struct数组的大小,如下所示:

  atom = new carbon[PARTICLE_NUM];

The problem is how I then use this struct in other files. 问题是我然后如何在其他文件中使用此结构。 I've created a header file and put this in it 我创建了一个头文件并将其放入其中

extern struct carbon *atom;

But it comes up with this error: 但是它会出现以下错误:

setup_pos.cpp:19: error: invalid use of incomplete type ‘struct carbon’
system_setup_distances.h:18: error: forward declaration of ‘struct carbon’

I know I shouldn't be using global variables, but I just want to test this out first. 我知道我不应该使用全局变量,但我只想先进行测试。 Thanks in advance for the help. 先谢谢您的帮助。

结构的定义需要在头文件中。

You need to include the definition of the structure carbon in a header file and then include that header file in files( .h or .cpp )where you are getting those incomplete type errors. 您需要在头文件中包含carbon结构的定义,然后将该头文件包含在文件( .h.cpp )中,在这些文件中您会遇到那些类型不完整的错误。

Why the Error? 为什么会出错?
Whenever you use a forward declaration, that type becomes an Incomplete type for the compiler, this is because the compiler knows only that the forward declared entity is an data type but it does not know anything about layout or its internals, so if you perform any operation which needs compiler to need the type layout it complains with the error. 每当使用前向声明时,该类型对于编译器都会成为不完整类型,这是因为编译器仅知道前向声明的实体是数据类型,但对布局或其内部一无所知,因此如果执行需要编译器需要它抱怨错误的类型布局的操作。

In Your case the compiler needs to know the sizeof the structure carbon to allocate enough memory, which it cannot since it is forward declared type and hence it complains with the error. 在您的情况下,编译器需要知道carbon结构的大小以分配足够的内存,因为它是前向声明的类型,所以编译器无法知道,因此它会报错。

The source file where you use atom needs the full definition of the carbon structure. 使用atom的源文件需要carbon结构的完整定义。

Put the structure together with the external in the same header file, like this: 将结构与外部一起放在相同的头文件中,如下所示:

struct carbon {
    double temp;
    double mass;
    rowvec::fixed<3> position;      
    rowvec::fixed<3> velocity;
    rowvec::fixed<3> force;
};

extern struct carbon *atom;

The define the variable atom in one of your source files: 在您的一个源文件中定义变量atom

struct carbon *atom = 0;

Now, whenever you need to access atom , include the header file where the structure and the extern declaration is, and it should work. 现在,每当您需要访问atom ,请在结构和extern声明所在的位置包含头文件,并且该文件应该可以工作。

PS. PS。 Instead of having the atom variable in the global namespace, you could put it in its own namespace: 您可以将其放在自己的名称空间中,而不是在全局名称空间中使用atom变量:

namespace some_clever_name
{
    struct carbon { ... };
    extern carbon *atom;
}

And put this in a source file: 并将其放在源文件中:

some_clever_name::carbon *some_clever_name::atom = 0;

As the other answers say, you need to include the definition of the struct in a header file. 就像其他答案所说的那样,您需要在头文件中包含该结构的定义。 But let's ask ourselves why you need this? 但是,让我们问自己为什么需要这个?

C++ has from the start been based on C, and inherits from C a simple compilation strategy: the compiler makes one pass and neither the compiler nor the linker should need to access anything except the file they're presented. C ++从一开始就基于C,并且从C继承了一种简单的编译策略:编译器通过了一次,并且编译器和链接器都不需要访问任何文件(除了提供的文件之外)。 When C and UNIX were first developed, address spaces were limited and processors were slower than most people can imagine -- my Kindle Fire is rather a bigger computer than anything I used until the 90's. 最初开发C和UNIX时,地址空间受到限制,并且处理器的速度比大多数人想象的要慢-我的Kindle Fire的计算机比90年代以前的任何产品都要大。

Because they were making the compiler simple, instead of using more complex schemes like PL/I (one of the ancestors of C) did, they built the preprocessor and use include files. 因为他们使编译器变得简单,而不是像PL / I(C的祖先之一)那样使用更复杂的方案,所以他们构建了预处理器并使用了include文件。 The compiler needs to know the "shape" of the structure so it can generate code -- for example if you want to access mass , you need to know the offset from the beginning of the struct. 编译器需要知道结构的“形状”,以便它可以生成代码-例如,如果要访问mass ,则需要知道从结构开头的偏移量。 So, in C and C++, you need to include, textually, the description of that "shape". 因此,在C和C ++中,您需要在文本上包括对该“形状”的描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM