Jump to content
  • Advertisement
Sign in to follow this  
d h k

[SIMPLE] Splitting up code in different files

This topic is 4870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everybody, here I am back with another very easy question: I want to write my 'Player' class in a new file called 'player.cpp'. So that's what I did: main.cpp

#include "player.h"


player.cpp

#include "player.h"

class CPlayer
{
	public:
		int		position_x;
		int		position_y;
		int		offset_x;
		int		offset_y;
		int		jump_velocity;
		int		health;
		bool	shooting;

		CSpriteBase		sprite_base;
		CSprite			sprite;

		void DrawHealthbar ( void );
};

player.h

extern CPlayer	player;


This is exactly how my c++ book says it should be done, but it does not work. It always returns 'error C2146: Syntaxerror : Missing ';' before 'player' when I try to use the class (access it) from main.cpp... What am I doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
Almost


player.h

class CPlayer { stuff }; // Must come before any CPlayer variable definition

extern CPlayer player; // Must come before any use of player


player.cpp

#include "player.h"

CPlayer player; // Actually creates the player variable


See Kylotan's organising code files article.

Share this post


Link to post
Share on other sites
First of all, place your class declaration in Player.h . Then, place your class implemenation in Player.cpp . You can have the extern CPlayer player; in Player.h , but do it after the class declaration. However, if you're doing that, you must have player declared somewhere else (assumably Player.cpp).

Here's a visual breakdown:

Main.cpp

#include "Player.h"

CPlayer player;



Player.h

class CPlayer
{
public:
int position_x;
int position_y;
int offset_x;
int offset_y;
int jump_velocity;
int health;
bool shooting;

CSpriteBase sprite_base;
CSprite sprite;

void DrawHealthbar ( void );
};

extern CPlayer player;



Player.cpp

#include "Player.h"

void CPlayer::DrawHealthbar(void)
{
...
}



Try reading your book again, or get a new book (that one seems bad)..
Hope this helps.

Share this post


Link to post
Share on other sites

No, your book don't tell you that. You'll want to check it again carefully.

1) the CPlayer class definition should be in the H file, not in the CPP file.
2) "extern CPlayer player;" should also be in the H file, after the class definition.
3) the CPP file should contain

#include "Player.h"
CPlayer player;

4) you'll probably need to add the correct includes to the player.h file - if you don't, then both CSpriteBase and CSprite will be undefined, and your code will not compile.

There is an article somewhere on gamedev.net that deal with code management - the C++ "where to put why". Hélas I'm unable to find it tonight.

I'm incredibly slow. The article I cited is Kylotan's one, of course.

HTH

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!