0

I'm trying to code a simple collision detection function as part of the Engine class. The Player class is initialized in the Engine.cpp. I tried initializing it in the CollisionDetection.cpp instead, and in Engine.h but it still doesn't work.

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;

    

    
};

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;

};

CollisionDetection.cpp

#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;
}

Getting error message:

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

O'Neil
  • 3,790
  • 4
  • 16
  • 30
  • 4
    I do not think that is the full and complete error message. What does the rest of the error say? – abelenky Sep 16 '20 at 14:19
  • Which line is 20? – drescherjm Sep 16 '20 at 14:20
  • Your bug appears to be a circular include. Player.h includes Engine.h while Engine.h includes Player.h – drescherjm Sep 16 '20 at 14:22
  • Does this answer your question? [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – drescherjm Sep 16 '20 at 14:22
  • 1
    Don't `#include` files that you don't need. – molbdnilo Sep 16 '20 at 14:27
  • 1
    Engine.h includes Player.h and Player.h includes Engine.h – AndyG Sep 16 '20 at 14:31
  • Player.h does not need to `#include "Engine.h"`. Engine.h does not need to `#include "Player.h"` rather can instead do a forward declaration `class Player;`. Don't include what the file doesn't need. Do include what the file does directly need. Don't use indirect includes, that is the path to madness. – Eljay Sep 16 '20 at 14:52

1 Answers1

1

You have a circular dependency, engine.h includes player.h and vice versa. I think instead of #include "Player.h" you can just pre-declare it like class Player;

Nico van Bentum
  • 339
  • 1
  • 13