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

Author Topic: no default constructor exists for class "sf::Text" error while using the SFML 3.  (Read 87 times)

0 Members and 1 Guest are viewing this topic.

OxyK

  • Newbie
  • *
  • Posts: 1
    • View Profile
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 :)
« Last Edit: January 05, 2025, 09:42:44 pm by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11071
    • View Profile
    • development blog
    • Email
sf::Text doesn't have a default constructor anymore, as the error says and as it's mentioned in the migration guide.

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();
}
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/