Lime_Coconut
Lime_Coconut

Reputation: 1

Trying to code my very first game in C++, and getting syntax error:: identifier message

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

Upvotes: 0

Views: 82

Answers (1)

Nico van Bentum
Nico van Bentum

Reputation: 347

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;

Upvotes: 1

Related Questions