Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "error C2061: syntax error : identifier "?

Tags:

c++

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();
};
like image 397
aratn0n Avatar asked Apr 03 '13 08:04

aratn0n


3 Answers

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.

like image 180
juanchopanza Avatar answered Oct 18 '22 02:10

juanchopanza


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.

like image 8
Kiril Kirov Avatar answered Oct 18 '22 02:10

Kiril Kirov


Circular dependency or you're using a C compiler for C++ code

like image 2
Stocazzo Avatar answered Oct 18 '22 01:10

Stocazzo