You are not logged in.
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
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
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
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
Heh......Only if you know how many "flag_types" and "_table" types there were 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
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
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
oooooooooooooooooooo. so there is that shortcut.
Offline
Okay fixed and working
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