Skip to main content
gamedev.net
🔒 Locked

Struct Problems with VS.net 2003

Started by Ludi83 Sep 15, 2005 at 4:18 PM 5 replies 450+ views
Original Post
Ludi83
Ludi83
Hi, one short question: Why doesn't VS.Net 2003 compile this code:

		typedef struct tKeyName {
			
			std::string	   name;
			SDLKey		   sdlkey;
		} tKeyName;

		tKeyName KeyNames[] = {
		    
			{"a",           SDLK_a},
			{"LEFT",        SDLK_LEFT},
			{"RIGHT",       SDLK_RIGHT},
			{"UP",          SDLK_UP},
			{"DOWN",        SDLK_DOWN},
			{"ESC",         SDLK_ESCAPE},
			{"MOUSE1",      SDLK_WORLD_0},
			{"MOUSE2",      SDLK_WORLD_1},
			{"MOUSE3",      SDLK_WORLD_2},
			{"WHEELUP",     SDLK_WORLD_3},
			{"WHEELDOWN",   SDLK_WORLD_4},
			{"",            SDLK_LAST}
		};


This code compiles under gcc3. I get error C2059 and C2334. I would post the whole error messasge, but it isn't english :) Something like: unexpected token(s) preceding "{"; skipping visible function text Thanks for your help :)
perfectly_dark
perfectly_dark
Shouldn't it be
typedef struct{						std::string	   name;			SDLKey		   sdlkey;		} tKeyName;


That's how I've always seen it, though I'm not expert on this since I always use
struct x{...}


...just a thought
dave
dave
Stupid point maybe but i saw when looking up one of the error codes that someone had named the file .C instead of .CPP and got this problem, didn't specify which compiler though.

Another thought is shouldn't it be something like this:

typedef struct tKeyName {						std::string	   name;			SDLKey		   sdlkey;		} tKeyName;		tKeyName KeyNames[] = {		    			{std::string("a"),           SDLK_a},			{std::string("LEFT"),        SDLK_LEFT},			{std::string("RIGHT"),       SDLK_RIGHT},			{std::string("UP"),          SDLK_UP},			{std::string("DOWN"),        SDLK_DOWN},			{std::string("ESC"),         SDLK_ESCAPE},			{std::string("MOUSE1"),      SDLK_WORLD_0},			{std::string("MOUSE2"),      SDLK_WORLD_1},			{std::string("MOUSE3"),      SDLK_WORLD_2},			{std::string("WHEELUP"),     SDLK_WORLD_3},			{std::string("WHEELDOWN"),   SDLK_WORLD_4},			{std::string(""),            SDLK_LAST}		};


Try that ^^^

ace
Ludi83
Ludi83
Okay, thanks for your help, but it didn'n work. And that double tKeyName was a typo :)

I don't know why, but you can't initialize structs this way in classes, don't ask me why :)

So, I changed the Code to this

//Input.h		typedef struct {						const char*	   name;			SDLKey		   sdlkey;		} tKeyName;		tKeyName *KeyNames;//Input.cppbool CInput::Init() {		static tKeyName sKeyNames[] = {		    		"a",           SDLK_a,		"LEFT",        SDLK_LEFT,		"RIGHT",       SDLK_RIGHT,		"UP",          SDLK_UP,		"DOWN",        SDLK_DOWN,		"ESC",         SDLK_ESCAPE,		"MOUSE1",      SDLK_WORLD_0,		"MOUSE2",      SDLK_WORLD_1,		"MOUSE3",      SDLK_WORLD_2,		"WHEELUP",     SDLK_WORLD_3,		"WHEELDOWN",   SDLK_WORLD_4,		"",            SDLK_LAST	};	KeyNames = sKeyNames;	return true;}


Looks really bad, huh :) But, let's hope it works.
dave
dave
It won't, you do need the {} around the initialised elements.

Try to declare the struct like this:

struct myStructType
{
int a;
};

ace
Ludi83
Ludi83
Quote:
Original post by ace_lovegrove
struct myStructType
{
int a;
};



Doesn't work.

Strange, VS compiles the other code even without the braces, but I add them, looks nicer :)
dave
dave
send me the entire project and i'll see if i can sort it out.

My email is in my profile add me to msn if you wish.

ace

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.