views:

170

answers:

3

I have some C code that I have to port to C++. The code has a structure

struct A { 
    ...
    struct A * myPtr;
}

And now two global arrays are declared and initialized like this:

//Forward declaration of Unit
struct A Unit[10];

struct A* ptrUnit[2] = { Unit, Unit+7 };
struct A Unit[10] = { { .., &ptrUnit[0] }, 
                      ... };

Now while this works fine in C, it gives an error in C++ (variable redeclared). Aren't variables allowed to be forward-declared in C++?

+9  A: 

In C++, a variable declaration must be prefixed with extern:

extern A Unit[10];

// ...

A Unit[10] = { ... };

(Note that in C++ you can omit the leading struct.)

sbi
See [this answer](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration/1410632#1410632) for more details on declarations and definitions.
sbi
@sbi: your statement about `the same namespace` is not completely correct. A `struct A` definition has an implicit `typedef struct A A` as a consequence, if the identifier `A` is not defined in the same scope. Something like `struct A { ... } A;` is completely legal in C++, and then `A` (without `struct`) refers to the object. That this is possible, doesn't meant that this is good style, though. In POSIX you have that for example with the "sys/stat.h" header file that defines `struct stat` and the function `stat`.
Jens Gustedt
@Jens: I'm not sure on the legal details of this, but you seem to have a point. I edited my answer accordingly.
sbi
@sbi, @Jens Gustedt: They still live in different identifier spaces, and there is no implicit `typedef` (else the symbol would also be defined in the global identifier space). The difference between C and C++ here is that if a symbol is not located in the identifier space, it will also be looked up in the user defined types. There is a slightly longer explanation in this answer I wrote some time ago: http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions/1675446#1675446
David Rodríguez - dribeas
@David: thanks for the pointer. The rule for C++ is even worse than I thought.
Jens Gustedt
+1  A: 

C allows variables to be tenatively declared (I guess). C++ does not. Once 'Unit' has been defined, it cannot be redefined in the same scope

Chubsdad
+6  A: 

struct A Unit[10] is not a forward declaration of a variable. The term "forward declaration" normally refers to non-defining declarations, while struct A Unit[10] is a definition. So in your code you are defining Unit multiple times in the same source file. In C language it is allowed, since in C definitions without an initializer are tentative definitions the may occur many times in the same translation unit. In C++ there's no such thing as tentative definition. In C++ multiple definitions are always illegal.

If you want a genuine forward declaration for a variable, you have to use the keyword extern

extern struct A Unit[10];

This will work in both C and C++. However, this will give Unit external linkage as a side effect. If you need a variable with internal linkage, then you are out of luck in C++, since in C++ it is not possible to forward declare a variable with internal linkage. In C tentative definitions will still help you.

AndreyT
+1, answer well put
sellibitze