Question

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:

  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.

Was it helpful?

Solution

The source file where you use atom needs the full definition of the carbon structure.

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:

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.

PS. Instead of having the atom variable in the global namespace, you could put it in its own namespace:

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;

OTHER TIPS

The definition of the struct needs to be in the header file.

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.

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.

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. 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.

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. 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. So, in C and C++, you need to include, textually, the description of that "shape".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top