You are not logged in.

#1 2006-10-20 02:50:20

kizeren
Member
Registered: 2006-10-20
Posts: 5

array type has incomplete element type [solved]

Well, not quite sure where to ask this question.  I have tried some MUD sites.  All give two options for fixing the problem with this error.  Neither which seem to work.

First one is,

extern const struct flag_type *act_flags;
struct flag_type  {
...
};

Second is,

struct flag_type  {
...
};
extern const struct flag_type act_flags[];

I have tried both these and still does not comiple.

Now after searching for couple of hours on posts and the web, I found that this is the proper way for array declarations?

extern const struct flag_type *mprog_flags;

So now I have the correct syntax above and for the actual array I have:

const struct flag_type mprog_flags[] =
{
    {   "act",                  TRIG_ACT,               TRUE    },
    {   "bribe",                TRIG_BRIBE,             TRUE    },
    {   "death",                TRIG_DEATH,             TRUE    },
    {   "entry",                TRIG_ENTRY,             TRUE    },
    {   "fight",                TRIG_FIGHT,             TRUE    },
    {   "give",                 TRIG_GIVE,              TRUE    },
    {   "greet",                TRIG_GREET,             TRUE    },
    {   "grall",                TRIG_GRALL,             TRUE    },
    {   "kill",                 TRIG_KILL,              TRUE    },
    {   "hpcnt",                TRIG_HPCNT,             TRUE    },
    {   "random",               TRIG_RANDOM,            TRUE    },
    {   "speech",               TRIG_SPEECH,            TRUE    },
    {   "exit",                 TRIG_EXIT,              TRUE    },
    {   "exall",                TRIG_EXALL,             TRUE    },
    {   "delay",                TRIG_DELAY,             TRUE    },
    {   "surr",                 TRIG_SURR,              TRUE    },
    {   NULL,                   0,                      TRUE    }
};

Since I have been told this is still the proper way to do the array itself.
But again when I compile I get more errors like below.

tables.c:493: error: conflicting types for 'mprog_flags'
tables.h:96: error: previous declaration of 'mprog_flags' was here
tables.c:542: error: conflicting types for 'exit_flags'
tables.h:95: error: previous declaration of 'exit_flags' was here
tables.c:569: error: conflicting types for 'room_flags'
tables.h:93: error: previous declaration of 'room_flags' was here

Now is my problem that I have the array declaration in seperate file as the actual array?  Or am I just completly missing it?

I am using GCC Version 4.1.1.

Thanks in advance for anyone who may know the answer and who take the time to read this post.
I will be more then happy to share the code if anyone would be interested in helping me.

Offline

#2 2006-10-20 05:13:13

tardo
Member
Registered: 2006-07-15
Posts: 526

Re: array type has incomplete element type [solved]

extern const struct flag_type *mprog_flags; 

I think that the only time you'd use extern is when accessing a function/variable in another file.

const struct flag_type mprog_flags[] =
{
    {   "act",                  TRIG_ACT,               TRUE    },
    {   "bribe",                TRIG_BRIBE,             TRUE    },
    {   "death",                TRIG_DEATH,             TRUE    }, 
     ....
}

I believe this only works if you have a multi-dimensional array, in which case you don't.

I think what you're trying to do is

const struct flag_type mprog_flags[17];

mprog_flags[0].var1 = "act";
mprog_flags[0].var2 = TRIG_ACT;
mprog_flags[0].var3 = TRUE;

mprog_flags[1].var1 = "bribe";
mprog_flags[1].var2 = TRIG_BRIBE;
mprog_flags[1].var3 = TRUE;

........

We might be able to help you better if we knew the contents of the struct, and what exactly you're trying to achieve.

Offline

#3 2006-10-20 11:22:49

kizeren
Member
Registered: 2006-10-20
Posts: 5

Re: array type has incomplete element type [solved]

And forgive me been about a year since I tried to program a game.
So I hope this is what your looking for.


struct flag_type
{
    char *name;
    int bit;
    bool settable;
};

This snip was taken out of the same file as array declaration.

extern const struct flag_type *mprog_flags; 

Offline

#4 2006-10-20 11:39:43

tardo
Member
Registered: 2006-07-15
Posts: 526

Re: array type has incomplete element type [solved]

const struct flag_type mprog_flags[17];

mprog_flags[0].name = malloc(10);
strcopy(mprog_flags[0].name,"act");
mprog_flags[0].bit = TRIG_ACT;
mprog_flags[0].settable = TRUE;

mprog_flags[1].name = malloc(10);
strcopy(mprog_flags[1].name,"bribe");
mprog_flags[1].bit = TRIG_BRIBE;
mprog_flags[1].settable = TRUE;
.....

is the way I would do it.

Offline

#5 2006-10-20 13:21:39

kizeren
Member
Registered: 2006-10-20
Posts: 5

Re: array type has incomplete element type [solved]

Heh......Only if you know how many "flag_types" and "_table" types there were smile   Anyway looks like I may have some very long nights ahead of me.

So now before I go changing the code.  Let me see if I get this.

The 17 is the "max_amount" of flags?
Then in the mprog_flags[0]  Would be the first starting number.

Thanks for all the help.

Offline

#6 2006-10-20 15:39:03

tardo
Member
Registered: 2006-07-15
Posts: 526

Re: array type has incomplete element type [solved]

The 17 is the "max_amount" of flags?

Yes. In your case, I counted the example you gave and used that number. You can use any sane number for arrrays.

Then in the mprog_flags[0] Would be the first starting number.

Yes again. 0 is the element in the array, 16 is the last. Anything outside that range will (usually) segfault.

struct flag_type
{
    char name[10]; // This part changed
    int bit;
    bool settable;
}; 

I noticed that you're using const, so I'm assuming you're not going to be changing the values. Instead of using a pointer and having to worry about alloc'ing and dealloc'ing, just make the string a fixed length (sacrificing a bit more memory).

Also, if you have lots of these flags/tables, I highly suggest reading them in from a file. It might save you from a lot of headaches.

Offline

#7 2006-10-20 16:46:43

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: array type has incomplete element type [solved]

I'm not really sure what's going on here, but your original way of doing it was close... the problem is your switching types in your extern decl.  A foo* is not the same declaration as a foo[].

You can safely declare the struct array as extern in a header as long as it has linkage (as long as the array is actually defined somewhere in a .c file)

foo.h

struct foo
{
    int a;
    int b;
    char c[100];
};
extern struct foo uber[];

foo.c

#include <string>
#include <stdio>
#include "foo.h"

struct foo uber[] =
{
    { 1, 6, "hello" },
    { 2, 5, "hello" },
    { 3, 4, "hello" },
    { 4, 3, "hello" },
    { 5, 2, "hello" },
    { 6, 1, "hello" },
    { 0, 0, "" }
};

int main()
{
    for(int i=0; strlen(uber[i].c) > 0; ++i)
    {
        printf("{ %d, %d, %s }n", uber[i].a, uber[i].b, uber[i].c);
    }
}

Offline

#8 2006-10-20 17:28:02

tardo
Member
Registered: 2006-07-15
Posts: 526

Re: array type has incomplete element type [solved]

oooooooooooooooooooo. so there is that shortcut.

Offline

#9 2006-10-21 00:54:46

kizeren
Member
Registered: 2006-10-20
Posts: 5

Re: array type has incomplete element type [solved]

Okay fixed and working smile
I did just as phrakture said and all is well now.

Basically I put everything back to the way it was.  All I had to do was move the code around.
From:

extern struct foo uber[];

struct foo
{
      int a;
      int b;
      char c[100];
};

To this:

struct foo
{
     int a;
     int b;
     char c[100];
};
extern struct foo uber[];

Thanks again for all the help.
Who would of thought I have been putting this code together wrong and it took an update of GCC to point it out.

Offline

Board footer

Powered by FluxBB