c++ - 尝试用C++编写我的第一个游戏,并获得语法错误::identifier message

标签 c++ syntax-error

我正在尝试将一个简单的碰撞检测功能编码为Engine类的一部分。 Player类在Engine.cpp中初始化。我尝试在CollisionDetection.cpp和Engine.h中初始化它,但仍然无法正常工作。
引擎

#pragma once
#include "Cupcake.h"
#include "Player.h"
#include "FrameTimer.h"

class Engine
{
public:
    //Constructor
    Engine();

    //run all the private functions
    void run();


private:
    //Private functions. Run will call all private functions 
    void input();
    void draw();
    bool detectCollisions(Player& p, Cupcake& c);


    //The usual RenderWindow
    sf::RenderWindow m_Window;

    //Declare a sprite and a texture for the background
    sf::Sprite m_BackgroundSprite;
    sf::Texture m_BackgroundTexture;

    //How many seconds, in gametime, have passed?
    FrameTimer ft;
    const float m_dt = ft.Mark();

    //Handle mouse input
    bool m_mouseHeld = false;
    //Mouse Positions
    sf::Vector2i m_mousePosition_Window;
    sf::Vector2f m_mousePosition;
    void updateMousePosition();

    sf::FloatRect m_mouseRect;

    //Is the game currently playing?
    bool m_Playing = false;

    //How much time has passed in-game?
    sf::Time m_GameTimeTotal;

    

    
};
播放器
#pragma once
#include <SFML/Graphics.hpp>
#include "Engine.h"

class Player 
{
public:
    //Constructor/Destructor
    Player();
    ~Player();
    
    //Getting functions
    int getHealth() { return m_health; }
    int getPoints() { return m_points; }
    //sf::FloatRect getMousePosition();
    
    //Check if player is still alive
    bool isAlive();

    //Reduce the player health
    int decreaseHealth() { m_health -= 1; return m_health; }
    int increasePoints() { m_points += 1; return m_points; }


private:

    //Game logic
    unsigned int m_points;
    int m_health;
    bool m_dead;

};
碰撞检测
#pragma once
#include <iostream>
#include <vector>


#include "Engine.h"
#include "Cupcake.h"
#include "Player.h"

using namespace sf;
Player player;

bool Engine::detectCollisions(Player& p, Cupcake& c)
{
    bool deleted = false;
    /*Use the intersects function from SFML to :
       -check if cupcake has been clicked upon
       -add points to the player
       -delete the cupcake sprite from the spriteVector
    */

    FloatRect mouse = m_mouseRect;

    if (c.getPosition().intersects((mouse)))
    {
        p.increasePoints();
        //find and delete the cupcake from the Sprite vector
        //turn deleted to true
        deleted = true;
    }


    return deleted;
}
收到错误消息:

syntax error: identifier 'Player' File: Engine.h Line:20

最佳答案

您具有循环依赖项,engine.h包含player.h,反之亦然。我认为您可以像#include "Player.h"一样预先声明而不是class Player;

关于c++ - 尝试用C++编写我的第一个游戏,并获得语法错误::identifier message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63922008/

相关文章:

c++ - 我如何摆脱逗号运算符的右操作数无效的错误(wunused 值)?

c++ - 在另一个函数中使用返回的数组指针

python - Kivy Pong教程语法错误: invalid syntax

c++ - Tensorflow C++ 内存泄漏 - Valgrind

C++/LapackE 代码在 Windows 上编译正常,但相同的代码在 Linux 上编译失败

php - 使用 OO-PHP 简单登录

php - 解析错误: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting T_VARIABLE [duplicate]

php mailgun 错误意外)

C++ 重载运算符不修改原始对象(通过引用传递)

C++ Visual Studio 2008,将项目从 64 位移动到 32 位