Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Having problems with forward declarations of SFML classes  (Read 2859 times)

0 Members and 1 Guest are viewing this topic.

nyenye

  • Newbie
  • *
  • Posts: 10
    • View Profile
Having problems with forward declarations of SFML classes
« on: March 24, 2017, 06:36:31 pm »
Hi all! TBH I'm a newbie when it comes to C++, also SFML is the first attempt at a C++ framework for games (have used LibGDX and Love2D before thought). Have been using them for 2 days :D just recently.

Then, right now I'm making a Pong clone, and while trying to make custom classes, right now Game, Ball and Paddle, I try to forward declare classes from SFML, but the thing is I don't exactly know how to  :-\. As an example here is the PongGame class definition and implementation:

PongGame.hpp
#ifndef PONGGAME_H
#define PONGGAME_H

// INCLUDES
#include "PongBall.hpp"
#include "PongPaddle.hpp"


//FORWARD DECLARE
class Event;
class Time;
class RenderWindow;

// CLASS
class PongGame
{
    private:
        /* fields */
        PongBall ball;
        PongPaddle paddle_left;
        PongPaddle paddle_right;

    public:
        PongGame();
        void processEvent(const sf::Event& event);
        void update(const sf::Time& elapsed);
        void draw(const sf::RenderWindow& window);
};

#endif
 

PongGame.cpp:
#include "PongGame.hpp"

#include <SFML/System/Time.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RenderWindow.hpp>

PongGame::PongGame()
{
    this->ball = PongBall();
    this->paddle_left = PongPaddle(/*PongPaddle::Player::Left*/);
    this->paddle_right = PongPaddle(/*PongPaddle::Player::Right*/);
}

void PongGame::processEvent(const sf::Event& event)
{
}

void PongGame::update(const sf::Time& elapsed)
{
}

void PongGame::draw(const sf::RenderWindow& window)
{
}
 

Thanks for any help!

P.S: And yep... I regret preceding each custom class with the word Pong.
« Last Edit: March 24, 2017, 06:38:31 pm by nyenye »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Having problems with forward declarations of SFML classes
« Reply #1 on: March 24, 2017, 06:40:56 pm »
You need to put them in the sf namespace.

namespace sf {
    class Event;
    // ...
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

nyenye

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Having problems with forward declarations of SFML classes
« Reply #2 on: March 24, 2017, 06:45:13 pm »
Thanks a lot!!! Now that's working fine, let's solve more problems :D

nyenye

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Having problems with forward declarations of SFML classes
« Reply #3 on: March 24, 2017, 07:04:07 pm »
Sorry to bother again. But I'm having trouble with the class Rect, which is given a template. In my case it would be FloatRect (https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Rect.php). I've tried:

namespace sf {
    class Rect<float>;
}
 

but without luck  :-\

Edit: Sorry to miss inform. Not without luck but with the follwoing error:

error: specialization of ‘sf::Rect<float>’ after instantiation
     class Rect<float>;
« Last Edit: March 24, 2017, 07:06:43 pm by nyenye »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Having problems with forward declarations of SFML classes
« Reply #4 on: March 24, 2017, 08:07:15 pm »
It's a C++ issue not an SFML issue, so a simple Google search will give you lots of answers.

You just need to specify all the template arguments. Since that's a bit verbose, I tend to just include the header. Chances are high that you might end up using a member variable Rect anyways. But that's just my personal way of doing things. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Having problems with forward declarations of SFML classes
« Reply #5 on: March 24, 2017, 08:12:46 pm »
Although it's a very good habit, if you're a newbie in C++ don't waste too much time on trying to forward-declare everything. It won't change your life if you include Rect.hpp because you don't know how to forward declare the template (and even if you find out, you won't be able to use the IntRect/FloatRect typedefs anyway).

Plus, don't overuse const references. Lightweight types such as sf::Time (and possibly sf::Event) can perfectly be passed by copy. That makes the function prototypes less verbose.
Laurent Gomila - SFML developer

nyenye

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Having problems with forward declarations of SFML classes
« Reply #6 on: March 25, 2017, 01:31:10 am »
Thanks for the tips to the both of you. I'll keep that in mind then! Truth is I still don't have thr hang of it yet, and probably will remain like this for a while. Got some books to check out!

jamesL

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: Having problems with forward declarations of SFML classes
« Reply #7 on: April 01, 2017, 12:08:57 pm »
take these

#include <SFML/System/Time.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RenderWindow.hpp>

out of PongGame.cpp

and put them in PongGame.hpp

PongGame.cpp includes  PongGame.hpp so it will still see those lines


and once they're in PongGame.hpp there is no need for
//FORWARD DECLARE
class Event;
class Time;
class RenderWindow;

so just delete them