Skip to main content
GameDev.net gamedev.net
🔒 Locked

GCC Syntax Errors Link Order Problem?

Started by EnigmaticCoder Dec 10, 2009 at 10:34 PM 4 replies 1.3k views
Original Post
EnigmaticCoder
EnigmaticCoder
Description Resource Path Location Type expected class-name before '{' token Enemy.h line 12 C/C++ Problem expected class-name before '{' token Laser.h line 13 C/C++ Problem expected class-name before '{' token Player.h line 14 C/C++ Problem make: *** SourceFiles/Main.o] Error 1 Hidden Cosmos line 0 C/C++ Problem I get the above errors in the following files:

#ifndef LASER_H_
#define LASER_H_

#include <map>
#include <string>
#include "Sprite.h"
#include "Player.h"
#include "Enemy.h"

class Player;
class Enemy;
class Laser : public Sprite
{
private:
	float xVelocity;
	float yVelocity;
	bool playerLaser;
public:
	Laser();
	Laser(const Laser& laser);
	Laser(std::string initialImagePath, float initialImageWidth, float initialImageHeight, float initalX, float initialY, float initialZ, float initialWidth, float initialHeight, float initialXVelocity, float initialYVelocity, bool initialPlayerLaser);
	void move(float xVelocity, float yVelocity, std::vector<Player> &players, std::vector<Enemy> &enemies);
	void move(std::vector<Player> &players, std::vector<Enemy> &enemies);
	void setVelocities(float newXVelocity, float newYVelocity);
	void setPlayerLaser(bool newPlayerLaser);
	bool getPlayerLaser();
};

#endif




#ifndef ENEMY_H_
#define ENEMY_H_

#include "Sprite.h"
#include "Player.h"
#include "Laser.h"
#include "Path.h"

class Player;
class Laser;
class Enemy : public Sprite
{
private:
	int currentPointIndex;
	bool isFinishedOnPath;
public:
	Enemy();
	Enemy(std::string initialImagePath, float initialImageWidth, float initialImageHeight, float initalX, float initialY, float initialZ, float initialWidth, float initialHeight);
	void move(float xVelocity, float yVelocity, std::vector<Player> &players, std::vector<Laser> &lasers);
	void moveToPoint(Point p, std::vector<Player> &players, std::vector<Laser> &lasers);
	void moveOnPath(Path &path, std::vector<Player> &players, std::vector<Laser> &lasers);
	bool finishedPath(Path currentPath);
	void shoot(float xVelocity, float yVelocity, std::vector<Laser> &lasers);
};

#endif




#ifndef PLAYER_H_
#define PLAYER_H_

#include <map>
#include <string>
#include "Globals.h"
#include "Sprite.h"
#include "Enemy.h"
#include "Laser.h"

class Enemy;
class Laser;
class Player : public Sprite
{
private:
	int lives;
	int invincibilityTicks;
public:
	Player();
	Player(std::string initialImagePath, float initialImageWidth, float initialImageHeight, float initalX, float initialY, float initialZ, float initialWidth, float initialHeight, int initialLives);
	void move(float xVelocity, float yVelocity, std::vector<Enemy> &enemies, std::vector<Laser> &lasers);
	void shoot(float xVelocity, float yVelocity, bool left, std::vector<Laser> &lasers);
	int getLives() const;
	void setLives(int newLives);
	void setDead(bool newDead);
	void resetCoordinates();
	void incrementInvincibilityTicks();
	bool invincible();
};

#endif


However, I didn't change these files at all and they used to build. Also, depending on the link order of my object files, I get different errors, mainly undefined references and such. To add more oddity to the problem, if I build the program, with //CODE1, rebuild with //CODE2, and then rebuild with //CODE1, it compiles and runs!

//CODE1
#ifndef GAME_STATE_H_
#define GAME_STATE_H_

#include <kos.h>
#include <map>
#include <vector>
#include <string>
#include "Player.h"
#include "Laser.h"
#include "Enemy.h"
#include "Level.h"
#include "Path.h"
#include "InputState.h"
#include "Init.h"

class GameState
{
private:
	int numberOfPlayers;
	bool paused;
	int currentLevel;
	int maxLevels;
	int screen;

public:
	GameState();
	GameState(int initialNumberOfPlayers, bool initialPaused, int initialCurrentLevel, int initialMaxLevels);
	void setNumberOfPlayers(int newNumberOfPlayers);
	int getNumberOfPlayers() const;
	void setPaused(bool newPaused);
	bool getPaused() const;
	void nextLevel();
	int getCurrentLevel() const;
	void setMaxLevels(int newMaxLevels);
	int getScreen() const;
	void setScreen(int newScreen,
			pvr_ptr_t &titleScreenImage,
			pvr_ptr_t &cursorImage,
			InputState &inputState,
			std::map<const std::string, pvr_ptr_t> &images,
			std::vector<Player> &players,
			std::vector<Laser> &lasers,
			std::vector<Enemy> &enemies,
			std::vector<Level> &levels,
			std::vector<Path> &paths);
};

#endif




//CODE2
#ifndef GAME_STATE_H_
#define GAME_STATE_H_

#include <kos.h>
#include <map>
#include <vector>
#include <string>

class GameState
{
private:
	int numberOfPlayers;
	bool paused;
	int currentLevel;
	int maxLevels;
	int screen;

public:
	GameState();
	GameState(int initialNumberOfPlayers, bool initialPaused, int initialCurrentLevel, int initialMaxLevels);
	void setNumberOfPlayers(int newNumberOfPlayers);
	int getNumberOfPlayers() const;
	void setPaused(bool newPaused);
	bool getPaused() const;
	void nextLevel();
	int getCurrentLevel() const;
	void setMaxLevels(int newMaxLevels);
	int getScreen() const;
};

#endif



Any one have a clue what's wrong?
--------------------Enigmatic Coding
owl
owl
You're including a .h file into itself through another one.

you are including enemy.h in laser.h and then you include laser.h into enemy.h

You can't do that in c++. To accomplish this you need to forward declare the classes in the .h file and then include the .h of that class into the cpp file.

All the definitions using forward declared classes must go into the cpp or incomplete type definition error is thrown.


//enemy.hclass laser;class enemy{  laser the_laser;};


//enemy.cpp#include "laser.h"// Method definitions


BTW, just in case you don't know (if you know don't worry then), this is a compile error. Compiling is making the source into binary. Linking is putting all the binary objects together to make an executable/library.
[size="2"]I like the Walrus best.
owl
owl
So you're doing it wrong.
[size="2"]I like the Walrus best.
Zao
Zao
Quote:
Original post by owl
So you're doing it wrong.

I'd not be too quick at handing out blame…

Your code snippet tries to use a forward declared type to declare a member variable.
The things you can do with an incomplete type T like that are limited and include declaring variables of pointer-to-T and reference-to-T.

In order to declare a member variable, the type must be complete.
To make it is hell. To fail is divine.

Topic Locked

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

Sign in to reply to this topic.