When I compile there is an error call "error C2061: syntax error : identifier 'Player' "
I can't figure out what is wrong with my code. Here is my code
#include "Platform.h" #include "Player.h" class Collision { public: Collision(void); ~Collision(void); static bool IsCollision(Player &player, Platform& platform); };
There is an error in "IsCollision" method.
Player.h
#include <SFML/Graphics.hpp>
#include "rapidxml.hpp"
#include <fstream>
#include <iostream>
#include "Collision.h"
using namespace rapidxml;
class Player
{
private:
sf::Texture playerTexture;
sf::Sprite playerSprite;
sf::Vector2u position;
sf::Vector2u source;
sf::Vector2u size;
int frameCounter, switchFrame, frameSpeed;
int walkSpriteWidth;
float velocity;
bool isWalk;
bool isStand;
bool isFaceRight;
public:
Player(void);
~Player(void);
void Init();
void Draw(sf::RenderWindow *window);
void MoveForward();
void MoveBackward();
void Update(sf::Clock *clock);
void SetSourceY(int value);
void SetWalk(bool value);
void SetFacing(std::string value);
void SetStand(bool value);
void Stand();
std::string GetStatus();
void PrintStatus();
sf::Vector2f GetPosition();
int GetWidth();
int GetHeight();
};
You have a circular include dependency. Collision.h includes Player.h and vice versa. The simplest solution is to remove #include "Collision.h"
from Player.h
, since the Collision
class is not needed in the Player
declaration. Besides that, it looks like some of your includes in Collision.h
can be replaced by forward declarations:
// forward declarations
class Player;
class Platform;
class Collision
{
public:
Collision(void);
~Collision(void);
static bool IsCollision(Player &player, Platform& platform);
};
You can then put the includes in Collision
's implementation file.
That's a pretty common mistake - you have circular include dependency.
Looking at your code, you should replace #include "Player.h"
with class Player;
in Collision.h
. This is called "forward declaration" and will break the circular dependency.
Also, it would be good to add include guards, for example:
#ifndef MY_PLAYER_CLASS
#define MY_PLAYER_CLASS
...
#endif
And this should be done for each header you write.
Circular dependency or you're using a C compiler for C++ code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With