Gameboy Development Forum

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.

Ads

#1 2014-06-07 08:27:39

Raildex
New member
Registered: 2014-06-07
Posts: 3

Structs

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:

Code:

#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

 

#2 2014-06-15 06:05:18

Tauwasser
Member
Registered: 2010-10-23
Posts: 160

Re: Structs

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.

Code:

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__ */

Code:

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

 

#3 2014-06-15 12:21:20

Raildex
New member
Registered: 2014-06-07
Posts: 3

Re: Structs

Thx, but the compiler has a problem with the "." connector.

Offline

 

#4 2014-07-12 17:32:15

Tauwasser
Member
Registered: 2010-10-23
Posts: 160

Re: Structs

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

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson