SFML community forums

Help => Graphics => Topic started by: OxyK on January 05, 2025, 04:05:12 pm

Title: no default constructor exists for class "sf::Text" error while using the SFML 3.
Post by: OxyK on January 05, 2025, 04:05:12 pm
Hello! Sorry, if this is a dumb question, I'm still learning! :)

I'm trying to implement Font/Text with SFML 3.0.0.

Game.h


#pragma once

#include <iostream>
#include <vector>
#include <ctime>
#include <sstream>

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>

//Class that acts as the game engine.

class Game
{
private:
        //Variables
        //Window
        sf::RenderWindow* window;

        //Mouse positions
        sf::Vector2i mousePosWindow;
        sf::Vector2f mousePosView;

        //Resources
        sf::Font font;

        sf::Text uiText;
       
        //Game logic
        bool endGame;
        unsigned points;
        float enemySpawnTimer;
        float enemySpawnTimerMax;
        int maxEnemies;
        bool mouseHeld;
        int health;

        //Game objects
        std::vector<sf::RectangleShape> enemies;
        sf::RectangleShape enemy;



        //Private functions
        void initVariables();
        void initWindow();
        void initFonts();
        void initText();
        void initEnemies();

public:
        //constructors, destructors
        Game();
        virtual ~Game();

        //Accessors
        const bool running() const;
        const bool getEndGame() const;

        //Functions
        void spawnEnemy();
        void pollEvents();
        void updateMousePositions();
        void updateText();
        void updateEnemies();
        void update();

        void renderText(sf::RenderTarget& target);
        void renderEnemies(sf::RenderTarget& target);
        void render();
};
 

Game.cpp

void Game::initFonts()
{
    this->font.openFromFile("Fonts/apercumovistarbold.ttf");
}

void Game::initText()
{
    this->uiText.setFont(this->font);
    this->uiText.setCharacterSize(24);
    this->uiText.setFillColor(sf::Color::White);
    this->uiText.setString("None");
}

Game::Game()
{
        this->initVariables();
        this->initWindow();
    this->initFonts();
    this->initText();
    this->initEnemies();
}

Game::~Game()
{
        delete this->window;
}


(I included only important parts of the code for this problem).

The error I'm getting is:
"no default constructor exists for class "sf::Text"




Any help would be appreciated :)
Title: Re: no default constructor exists for class "sf::Text" error while using the SFML 3.
Post by: eXpl0it3r on January 05, 2025, 09:45:40 pm
sf::Text doesn't have a default constructor anymore, as the error says and as it's mentioned in the migration guide (https://www.sfml-dev.org/tutorials/3.0/getting-started/migrate/#removal-of-default-constructors).

That means, that you need to pass in a font at the text construction, which can be done in the init list of your class:

Game::Game() : uiText(font)
{
        this->initVariables();
        this->initWindow();
    this->initFonts();
    this->initText();
    this->initEnemies();
}