2013-04-03 76 views
0

當我編譯有錯誤調用「錯誤C2061:語法錯誤:標識符‘球員’」什麼是「錯誤C2061:語法錯誤:標識符」?

我無法弄清楚什麼是錯我的代碼。這裏是我的代碼

#include "Platform.h" 
#include "Player.h" 
class Collision 
{ 
public: 
    Collision(void); 
    ~Collision(void); 
    static bool IsCollision(Player &player, Platform& platform); 
}; 

有一個在 「IsCollision」 方法的錯誤。

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(); 
}; 
+3

向我們展示你的'Player.h',請 –

+3

最有可能的是,'Player'在另一個命名空間中,或者更可能的是''Player.h'包含了包含這個代碼的頭文件。 –

+0

@KirilKirov後者似乎更可能(鑑於這是一個非常常見的錯誤)。您應該將其作爲答案發布。 –

回答

6

你有一個圓形包括依賴性。 Collision.h包含Player.h,反之亦然。最簡單的解決方案是從Player.h中刪除#include "Collision.h",因爲在Player聲明中不需要Collision類。除此之外,它看起來像你的一些在Collision.h包括可以在前面聲明所取代:

// forward declarations 
class Player; 
class Platform; 

class Collision 
{ 
public: 
    Collision(void); 
    ~Collision(void); 
    static bool IsCollision(Player &player, Platform& platform); 
}; 

然後,您可以把包括Collision的實現文件。

+0

謝謝。它現在有效。我從來不知道這條規則。 – aratn0n

2

這是一個很常見的錯誤 - 你有循環包括依賴。

看着你的代碼,你應該在Collision.h中用class Player;代替#include "Player.h"。這被稱爲「前向聲明」,將打破循環依賴。


而且,這將是很好的補充包括警衛,例如:

#ifndef MY_PLAYER_CLASS 
#define MY_PLAYER_CLASS 

... 

#endif 

這應該爲每個你寫的標題來完成。

相關問題