Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)
You are not logged in.
Hey there! I'm new to C and Gameboy Development.
I have a problem with a struct I want to create:
My File looks like this:
#ifndef MONSTERS_H #define MONSTERS_H typedef struct monster { char name[]; unsigned short hp,mp,exp,money; unsigned char level,str,def,agi,spr,rfx,hit; }Monster; Monster monsterDatabase[5]; /*Monster - 1 - Jelly Blob*/ monsterDatabase[0].name = "Jelly Blob"; monsterDatabase[0].hp = 20; monsterDatabase[0].mp = 6; monsterDatabase[0].level = 1; monsterDatabase[0].str = 3; monsterDatabase[0].def = 4; monsterDatabase[0].agi = 4; monsterDatabase[0].spr = 7; monsterDatabase[0].rfx = 4; monsterDatabase[0].hit = 7; /*Monster - 2 - Dive Bird*/ #endif
but the compiler gives an error message:
Game\\Monsters.h(12) parse error: token -> '.' ; column 27
Game\\Monsters.h(12):error *** conflict with previous definition of 'monsterData
base' for attribute 'type'
Game\\Monsters.h(12):warning *** previous defintion type array of struct monster
Game\\Monsters.h(12):warning *** current definition type array of int
Game\\Monsters.h(13) parse error: token -> '.' ; column 27
Game\\Monsters.h(13):error *** conflict with previous definition of 'monsterData
base' for attribute 'type'
Game\\Monsters.h(13):warning *** previous defintion type array of struct monster
Game\\Monsters.h(13):warning *** current definition type array of int
[...]
Does the Compiler compile structs at all?
Thanks in advance :]
Last edited by Raildex (2014-06-07 08:27:59)
Offline
You're trying to write code into your header... That's not how you would initialize a struct like that. The error messages are all confused, so that's no help either.
monsters.h #ifndef __MONSTERS_H__ #define __MONSTERS_H__ typedef struct monster { char *name; unsigned short hp,mp,exp,money; unsigned char level,str,def,agi,spr,rfx,hit; }Monster; #endif /* __MONSTERS_H__ */
monsters.c #include "monsters.h" Monster monsterDatabase[] = { { .name = "Jelly Blob", .hp = 20, .mp = 6, .exp = 42, .money = 9999, .level = 1, .str = 3, .def = 4, .agi = 4, .spr = 7, .rfx = 4, .hit = 7, }, };
Basically, you cannot use code that would index some array to initialize it globally. You can only do that inside a function (which would be a waste of time and space). Instead, use array initializers like I did above.
cYa,
Tauwasser
Offline
Thx, but the compiler has a problem with the "." connector.
Offline
You can just leave initialization by field name out and initialize by field order. Are you by any chance compiling as c++? Because c++ does not have the ability for named initialization.
cYa,
Tauwasser
Offline