C++ 尝试用C+编写我的第一个游戏+;,和获取语法错误::标识符消息
我正试图编写一个简单的碰撞检测函数作为Engine类的一部分。播放器类在Engine.cpp中初始化。我尝试在CollisionDetection.cpp中初始化它,并在Engine.h中初始化它,但它仍然不起作用 引擎C++ 尝试用C+编写我的第一个游戏+;,和获取语法错误::标识符消息,c++,syntax-error,C++,Syntax Error,我正试图编写一个简单的碰撞检测函数作为Engine类的一部分。播放器类在Engine.cpp中初始化。我尝试在CollisionDetection.cpp中初始化它,并在Engine.h中初始化它,但它仍然不起作用 引擎 #pragma once #include "Cupcake.h" #include "Player.h" #include "FrameTimer.h" class Engine { public: //C
#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;
};
Player.h
#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一次
#包括
#包括“发动机.h”
职业选手
{
公众:
//构造函数/析构函数
Player();
~Player();
//获取函数
int getHealth(){return m_health;}
int getPoints(){返回m_点;}
//sf::FloatRect getMousePosition();
//检查玩家是否还活着
bool isAlive();
//降低玩家的生命值
int decreaseHealth(){m_health-=1;返回m_health;}
int increasePoints(){m_points+=1;返回m_points;}
私人:
//博弈逻辑
无符号整数m_点;
国际卫生大学;
布尔穆死了;
};
碰撞检测
#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;
}
#pragma一次
#包括
#包括
#包括“发动机.h”
#包括“Cupcake.h”
#包括“Player.h”
使用名称空间sf;
玩家;
布尔引擎::检测碰撞(玩家和p、纸杯蛋糕和c)
{
bool deleted=false;
/*使用SFML中的intersects函数:
-检查是否点击了纸杯蛋糕
-给玩家加分
-从spriteVector中删除纸杯蛋糕精灵
*/
FloatRect鼠标=m_鼠标竖立;
如果(c.getPosition().intersects((鼠标)))
{
p、 增加点();
//从精灵向量中查找并删除纸杯蛋糕
//将删除变为真
删除=真;
}
返回已删除;
}
获取错误消息:
语法错误:标识符“Player”文件:Engine.h行:20
您有一个循环依赖项,engine.h包括player.h,反之亦然。我认为,除了
#包含“Player.h”
之外,您可以像class Player那样预先声明它代码>我认为这不是完整的错误信息。错误的其余部分是什么?哪一行是20?您的错误似乎是一个循环包含。Player.h包括Engine.h,而Engine.h包括Player.hDon不#包括不需要的文件。Engine.h包括Player.h和Player.h包括Engine.hThanks!我通过清理我的include和预先声明的Player类来实现它;