Struct initialization leads to linker error

J

John den Haan

Hello all!

In my game, I have a structure defined as follows:

typedef struct vegetation {
vegetation_type type;
bool harvested;
int wood_yield;
int fruit_content;
int asciiChar;
int color;
char description[];
} vegetation;

Later in the same header, I instantiate this type as follows:

vegetation VEG_NONE = {VEG_TYPE_NONE,false,0,0,0,0,"None"};

This is obviously required, since the flexible array-member cannot be
dynamically initialized.

However, the linker cokes on this with the following error message:

main.c: multiple definition of `VEG_NONE'
graphics.c: first defined here

Why is this? I have properly encapsulated the header file in an
#ifndef,#define, #endif structure. So what could be happening?

--

Cheers,

John den Haan
joDhn[dot]haEan[at]chLello[dot]nl

Remove capital 'DEL' from above addy to obtain e-mail address
 
J

John den Haan

Never mind, I sorted it out. Looks liek there was a hiatus in my
knowledge concerning multiple inclusion and the use of externs ;)

Thanks anyways
 
J

John den Haan

Well then, I got the linker error sorted out.

Now the compiler chokes on the following:

vegetation VEG_GRASS =
{VEG_TYPE_GRASS,false,0,0,ASCII_GRASS,COLOR_GRASS,"Grass"};

It tells me:

error: syntax error before ';' token

It seems to dislike the use of a #defined constant in the initialization
of the struct. What can I do to circumvent this problem? enums seem to
work (as shown by VEG_TYPE_GRASS, which is graciously accepted), but
#define's do not. I;ve tried explicit typecasting of the define, but it
doesn't do the job...
 
E

Eric Sosman

John said:
Well then, I got the linker error sorted out.

Now the compiler chokes on the following:

vegetation VEG_GRASS =
{VEG_TYPE_GRASS,false,0,0,ASCII_GRASS,COLOR_GRASS,"Grass"};

It tells me:

error: syntax error before ';' token

It seems to dislike the use of a #defined constant in the initialization
of the struct. What can I do to circumvent this problem? enums seem to
work (as shown by VEG_TYPE_GRASS, which is graciously accepted), but
#define's do not. I;ve tried explicit typecasting of the define, but it
doesn't do the job...

You have not provided enough information, but I'll guess
that you did something like

#define ASCII_GRASS 'G';
instead of
#define ASCII_GRASS 'G'
 
J

John den Haan

You have not provided enough information, but I'll guess
that you did something like

#define ASCII_GRASS 'G';
instead of
#define ASCII_GRASS 'G'

OMG. You're right :D

--

Cheers,

John den Haan
joDhn[dot]haEan[at]chLello[dot]nl

Remove capital 'DEL' from above addy to obtain e-mail address
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top