Structure Decalarations

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
dream_coder
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sat Mar 27, 2010 5:16 pm

Structure Decalarations

Post by dream_coder »

Hey all, I am creating my character stats using a structure. Do I need to declare this structure in the main source code or can I do it in my Header file, in case I want to use it again?
Image
Image
Image
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Structure Decalarations

Post by MrDeathNote »

dream_coder wrote:Hey all, I am creating my character stats using a structure. Do I need to declare this structure in the main source code or can I do it in my Header file, in case I want to use it again?
Yea you can put it in your header, it's fine if your gonna be reusing it.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
dream_coder
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sat Mar 27, 2010 5:16 pm

Re: Structure Decalarations

Post by dream_coder »

I have declared it in a different header file but VC++ wont let me use it. Below is the code of my functions, this is where I am trying to use the structure in the CreateChar function.

Code: Select all

//Header guards

#ifndef ADD_H 
#define ADD_H 

#include "cus_structures.h"


//Function header, these are the declarations of functions I am using to make the program more friendly
//and reusable, will probably upgrade to classes in the near future, 

int DrawMenu();
void CreateChar();
void LoadGame();
void NewGame();

//This is the code for the above functions, notice how easier it is to modify these seperatly 
//from the main program

int DrawMenu()
{
	
	//Define local variable for menu option.
	int intChoice;

	system("cls");	//Clear the screen
		cout << "Please select an option: ";
		cout << endl;
		cout << "0: Create Character: " << endl;
		cout << "1: Load Game: " << endl;
		cout << "2: New Game: " << endl;
		cout << "3: Exit: " << endl;

		cout << "Please enter your choice: ";
		cin >> intChoice;

		return intChoice;


}

void CreateChar()
{
	int Pause;
	//Character Creation code, create a character then write it to file so it can be used later in a game.
	Stats NewPlayer;
	NewPlayer.life = 10;
	NewPlayer.magic = 10;
	NewPlayer.attack = 10;
	NewPlayer.defence = 10;

	system("cls");
	cout << "Please enter a name for your character: ";
	cin >> NewPlayer.CharName;

	system("cls");
	cout << "Here are your stats: "; << endl;
	cout << "Name: " << NewPlayer.CharName << endl;
	cout << "life: " << NewPlayer.life << endl;
	cout << "magic: " << NewPlayer.magic << endl;
	cout << "attack: " << NewPlayer.attack << endl;
	cout << "defence: " << NewPlayer.defence << endl;

	cin << Pause;



}

void LoadGame()
{
	//Some code here
}

void NewGame()
{
	//Some code here
}

#endif
And here is the header file where I declare the structure

Code: Select all

#ifndef MY_STRUCTS
#define MY_STRUCTS

struct Stats {
	string CharName;
	int life;
	int magic;
	int attack;
	int defence;

};




#endif
When I run this code VC++ will not recognise the structure when I try and create it
1>c:\users\cyberstu\documents\visual studio 2008\projects\project_stu\project_stu\functions.h(46) : error C2065: 'Stats' : undeclared identifier
Image
Image
Image
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Structure Decalarations

Post by GroundUpEngine »

Yo, I ran this in VC++ it told me you had 44 errors, so I tryed it in Code::Blocks and it pointed out 2 obvious errors.
I'm not hating on VC++ "I'm just saying" :lol:

Line 60 -> Extra semi-colon

Code: Select all

cout << "Here are your stats: "; << endl;
Line 67 -> Operator is wrong way round

Code: Select all

cin << Pause;
Apart from that your code is fine ;)
dream_coder
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sat Mar 27, 2010 5:16 pm

Re: Structure Decalarations

Post by dream_coder »

Cool cheers for that. By the way, without sounding stupid, what is Code Blocks??
Image
Image
Image
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Structure Decalarations

Post by GroundUpEngine »

dream_coder wrote:Cool cheers for that. By the way, without sounding stupid, what is Code Blocks??
Just another IDE like VC++ ;)
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Structure Decalarations

Post by mv2112 »

GroundUpEngine wrote:Yo, I ran this in VC++ it told me you had 44 errors, so I tryed it in Code::Blocks and it pointed out 2 obvious errors.
I'm not hating on VC++ "I'm just saying" :lol:
Thats why i always fix the first error on the list and rebuild, usually it fixes all errors :mrgreen:
dream_coder
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sat Mar 27, 2010 5:16 pm

Re: Structure Decalarations

Post by dream_coder »

Fixed those two errors. Still not working in VC++. Gonna get Code Blocks now. Seems stupid that different compilers treat the same code differently.
Image
Image
Image
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Structure Decalarations

Post by avansc »

GroundUpEngine wrote:Yo, I ran this in VC++ it told me you had 44 errors, so I tryed it in Code::Blocks and it pointed out 2 obvious errors.
I'm not hating on VC++ "I'm just saying" :lol:

Line 60 -> Extra semi-colon

Code: Select all

cout << "Here are your stats: "; << endl;
Line 67 -> Operator is wrong way round

Code: Select all

cin << Pause;
Apart from that your code is fine ;)
Ummm, well there are a lot of things that contribute to this.
First of all, you probably have different warning level flags on the respective IDE's

For verbatim code, you ALL WAYS want to take the setup that gives you the most errors/warnings.

And lastly, the MS compiler is very very strict compared to GCC.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: Structure Decalarations

Post by thejahooli »

avansc wrote:And lastly, the MS compiler is very very strict compared to GCC.
Which is actually a good thing because it stops you from thinking your code is fine when there are actually problems with it.
I'll make your software hardware.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Structure Decalarations

Post by GroundUpEngine »

I agree with all the above, but the strictness of VC++ can be more annoying than helpful sometimes.

p.s. I set the warning/error flags real high in Code::Blocks so I get the feel of VC++ but I can still chill ;)
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Structure Decalarations

Post by Falco Girgis »

GroundUpEngine wrote:I agree with all the above, but the strictness of VC++ can be more annoying than helpful sometimes.

p.s. I set the warning/error flags real high in Code::Blocks so I get the feel of VC++ but I can still chill ;)
Seriously? I think GCC is TEN MILLION times bitchier than Visual Studio.

It bitches about no newlines at end of file (just because the C++ standard doesn't mention how to handle it. Common effing sense). It WONT LET ME do something like call a function Move(Vector2) with Move(pos1-pos2), because all intermediate stack variables are implicitly cast to const. It also doesn't let you make an enumerated type forward declaration? What the fuck?

I find GCC inferior to VC++ in many, many ways. It seems as though "just because the standard doesn't specifically mention every tiny thing" they don't even bother implementing it. I use GCC for the DC/PSP builds and VS2008 for Windows. I like VS a million times better.

When I build the engine, I usually have 0 errors and warnings on the PC and something like 20 warnings on the PSP/DC
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Structure Decalarations

Post by avansc »

GyroVorbis wrote:
GroundUpEngine wrote:I agree with all the above, but the strictness of VC++ can be more annoying than helpful sometimes.

p.s. I set the warning/error flags real high in Code::Blocks so I get the feel of VC++ but I can still chill ;)
Seriously? I think GCC is TEN MILLION times bitchier than Visual Studio.

It bitches about no newlines at end of file (just because the C++ standard doesn't mention how to handle it. Common effing sense). It WONT LET ME do something like call a function Move(Vector2) with Move(pos1-pos2), because all intermediate stack variables are implicitly cast to const. It also doesn't let you make an enumerated type forward declaration? What the fuck?

I find GCC inferior to VC++ in many, many ways. It seems as though "just because the standard doesn't specifically mention every tiny thing" they don't even bother implementing it. I use GCC for the DC/PSP builds and VS2008 for Windows. I like VS a million times better.

When I build the engine, I usually have 0 errors and warnings on the PC and something like 20 warnings on the PSP/DC
Yea, GCC is not even ISO/IEC C99 compliment, it has entire chunks missing of the standard.

Intel makes a fantastic compiler aswell just FYI.

ps: i know you know the difference between a compiler and IDE, but i see you compare gcc and visual studio with respect to the compilers, the compiler used in visual studio by default is cl.exe, just incase you didn't know the name.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Structure Decalarations

Post by GroundUpEngine »

@Gyro
I'm trying not to imply GCC is better in any way, shape, or form. It's just I have never experienced these things you speak of and I don't really use Visual Studio as much as I used too. Although after reading this, I'm tempted to port my engine back to VC++ and break out of some bad habits ;)

@avansc
hmm, from reading this it seems I like GCC because it's 'dirty' and I can get away with shit. Don't worry I'm not very proud of my self right now :lol:
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Structure Decalarations

Post by eatcomics »

The only problem I've had with VS is that I've not been able to get more than a simple window up when it comes to win32, I'm guessing its just something I'm doing wrong, but can't for the life of me figure it out... where as code::blocks and dev-cpp both compiled and ran fine, so IDK...
Image
Post Reply