Structures??

Started by
4 comments, last by Violento 22 years, 8 months ago
I have a problem with structures in my game. My project has these files vars.c and vars.h, where i declare stuff. I have the player''s structure in the vars.h like this... typedef struct{ int x,y; int speed; (and all the other player-stuff..) }player; And in the vars.c... static player pl; pl.x = 16; pl.y = 16; pl.speed = 1; I get the error "parse error before `.´" at the pl.x point. So my point is to add those values to the player structure, but i can see that i''m doing something wrong. I''m confused cause it worked before when i was programming with one .c-file only. So if somebody could tell me how to do this??
-Hombre Violento
Advertisement
your structure doesn''t have a name try changing it to this:

typedef struct player
{
int x,y;
int speed;
(and all the other player-stuff..)
}player;

also, if you are using C++, i reccomend classes.




X4J
X4J
Didn''t work..
Even when i tried it as simple as this...

vars.c :

struct{
int x,y;
int speed;
}pl;

pl.x = 16;
pl.y = 16;
pl.speed = 1;

...it gives me the same error. I guess i should put something in front of "pl.x = 16"?
-Hombre Violento
Your still not naming the structure.

struct player
{
int x,y;
int speed;
};

player myPlayer;

myPlayer.x = blah blah;
myPlayer.y = blah blah;
Joseph FernaldSoftware EngineerRed Storm Entertainment.------------------------The opinions expressed are that of the person postingand not that of Red Storm Entertainment.
Shouldnt need to name the structure... the original code looks like it should work....

Here are a few possibilities (I apologise if any of these are insultingly obvious, but sometimes even the best coders can overlook stupid mistakes)

1. You are actually #include ing the header arent you?

2. The references to pl.x and so on must be inside a function. If you want to initialise a global with certain values, use the {} notation...

static player pl = {16,16,1,blah blah};

3. Do you have any other definitions in the .h file? make sure they are all syntactically correct (no missing ;''s for example)

Hey! That "static player pl = {16,16,1,blah blah};" worked!

Well, that was new to me =). That''s why i posted this at the newbie section. I really should get some tutorials about this "multi-file"-programming.

Thanks!
-Hombre Violento

This topic is closed to new replies.

Advertisement