Sanity check - Identifier issue

Started by
8 comments, last by Mybowlcut 15 years, 10 months ago
I must be going crazy... I can't figure out why I'm getting these errors...
#ifndef RUDEY_ENEMY_H
#define RUDEY_ENEMY_H

#include "SDL/SDL.h"

#include "Cloneable.h"
#include "Direction.h"
#include "Drawable.h"
#include "SDL_Tools.h"
#include "Updateable.h"
#include "XImage.h"

#include "Rudey_Clock.h"
#include "Rudey_Level.h"

class Rudey_Enemy : public XImage
{
public:
	Rudey_Enemy();
	Rudey_Enemy(
		int speed,
		const std::string& name,
		const std::string& file_name,
		Point xy,
		bool is_colour_key,
		SDL_Colour colour_key = SDL_Tools::colour(255,255,255));
	virtual ~Rudey_Enemy();

	int Get_Speed() const;

	void Move(Direction::DIRECTION dir);
public:
	virtual void Draw();
	virtual void Update(const SDL_Event& event_);
	//virtual Rudey_Enemy* Clone() const;
private:
	int speed;
};

#endif
#include "Rudey_Enemy.h"

Rudey_Enemy::Rudey_Enemy()
	:
	speed(0)
{
}

Rudey_Enemy::Rudey_Enemy(
	int speed,
	const std::string& name,
	const std::string& file_name,
	Point xy,
	bool is_colour_key,
	SDL_Colour colour_key)
	:
	XImage(name, file_name, xy.x, xy.y, is_colour_key, colour_key),
	speed(speed)
{
	position.w = SDL_Tools::get_surface_size(surface).x;
	position.h = SDL_Tools::get_surface_size(surface).y;
}

Rudey_Enemy::~Rudey_Enemy()
{
}

int Rudey_Enemy::Get_Speed() const
{
	return speed;
}

void Rudey_Enemy::Move(Direction::DIRECTION dir)
{
	position = SDL_Tools::move_by(position, dir, Rudey_Level::TILE_WIDTH);
}

void Rudey_Enemy::Draw()
{
	Draw_Engine::Blit_Surface(position, surface, NULL);
}

void Rudey_Enemy::Update(const SDL_Event& event_)
{
	//if(Rudey_Clock::Get_Ticks() >= speed)
	//{
	//	// move some random position
	//}
}

//Rudey_Enemy*; Rudey_Enemy::Clone() const
//{
//	return new Rudey_Enemy(*this);
//}
#ifndef RUDEY_LEVEL_H
#define RUDEY_LEVEL_H

#include <vector>
#include <string>

#include "Rudey.h"
#include "Rudey_Clock.h"
#include "Rudey_Enemy.h"

class Rudey_Level
{
public:
	Rudey_Level();
	Rudey_Level(Rudey* rudey, int difficulty);
	~Rudey_Level();

	static int TILE_WIDTH, TILE_HEIGHT;
private:
	Rudey* rudey;
	int difficulty;
	std::vector<Rudey_Enemy> enemies;
};

#endif

#include "Rudey_Level.h"

int Rudey_Level::TILE_WIDTH = 25;
int Rudey_Level::TILE_HEIGHT = 25;

Rudey_Level::Rudey_Level()
	:
	rudey(NULL),
	difficulty(0)
{
}

Rudey_Level::Rudey_Level(Rudey* rudey, int difficulty)
	:
	rudey(rudey),
	difficulty(difficulty)
{
}

Rudey_Level::~Rudey_Level()
{
}

Quote:error C2146: syntax error : missing ';' before identifier 'r' 23 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 23 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 23
Cheers.

Advertisement
Which line of which file is the error on? (i.e. post the rest of the error message). You should be able to double-click the error message to go to that line.

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Sorry, it's in the Rudey_Level .h file... the vector of Rudey_Enemys. 23 I think... says it in the error at the end.

#ifndef RUDEY_ENEMY_H#define RUDEY_ENEMY_H#include "SDL/SDL.h"#include "Cloneable.h"#include "Direction.h"#include "Drawable.h"#include "SDL_Tools.h"#include "Updateable.h"#include "XImage.h"#include "Rudey_Clock.h"#include "Rudey_Level.h"class Rudey_Enemy : public XImage{public:	Rudey_Enemy();	Rudey_Enemy(		int speed,		const std::string& name,		const std::string& file_name,		Point xy,		bool is_colour_key,		SDL_Colour colour_key = SDL_Tools::colour(255,255,255));	virtual ~Rudey_Enemy();	int Get_Speed() const;	void Move(Direction::DIRECTION dir);public:	virtual void Draw();	virtual void Update(const SDL_Event& event_);	//virtual Rudey_Enemy* Clone() const;private:	int speed;};#endif

You have a circular inclusion error - RudeyEnemy.h includes RudeyLevel.h and RudeyLevel.h includes RudeyEnemy.h. The former include is unneccessary.

Σnigma
Oh... but I need Rudey_Level::TILE_WIDTH in Rudey_Enemy.cpp...

Quote:Original post by Mybowlcut
Oh... but I need Rudey_Level::TILE_WIDTH in Rudey_Enemy.cpp...


Then include it in the .cpp and not in the .h. In C++, you should move your inclusions in the source files (.cpp) whenever possible to avoid such problems.
See, this is what I never got... what is the difference between moving it to the .cpp file? I thought the .h and .cpp got merged together during compilation into a .obj file?

The difference is that every other file that #include your header will include automatically every header you #include there. If you #include an header in your c++ file you will not espose that header to others.

The general rule is: never include an header file in you header files. Include only those file that you are forced to use.

Example: you use a std::string object in you header -> you must #include "string"

Example: you use a reference to a c++ stream in your header -> you must not include iostream, fstream or others. Perhaps #include "iosfwd" will suffice. That particular header contains only typedefs of c++ standard headers and then is parsed very quickly.

An other important rule is: never assume that a module that can be build together with other modules can be compiled alone. Your header file "Rudey_Enemy.h" uses a std::string& but do not include the corresponding header. It compile probably because it's used in other headers the #include "string". You must include every headers in order to have modules that can be compiled alone in a graceful way.
--"Low level programming is good for the programmer's soul" -- John Carmack
This article goes over how to organize your code files.
Quote:Original post by zaerl
The difference is that every other file that #include your header will include automatically every header you #include there. If you #include an header in your c++ file you will not espose that header to others.

The general rule is: never include an header file in you header files. Include only those file that you are forced to use.

Example: you use a std::string object in you header -> you must #include "string"

Example: you use a reference to a c++ stream in your header -> you must not include iostream, fstream or others. Perhaps #include "iosfwd" will suffice. That particular header contains only typedefs of c++ standard headers and then is parsed very quickly.

An other important rule is: never assume that a module that can be build together with other modules can be compiled alone. Your header file "Rudey_Enemy.h" uses a std::string& but do not include the corresponding header. It compile probably because it's used in other headers the #include "string". You must include every headers in order to have modules that can be compiled alone in a graceful way.
I read the article but I didn't see anything mentioned about the rule that zaerl mentioned.

I moved the "Rudey_Level" include to the "Rudey_Enemy.cpp" file and it worked. Cheers for that! :D


This topic is closed to new replies.

Advertisement