c++classwindowoverloadingsfml

SFML - Trying to overload a window as a function parameter


I'm using SFML with C++ in Visual Studio 2019. I wanted to try to make a function to streamline the process of printing text to the window.

#include "SFML/Graphics.hpp"
#include "SFML/Window.hpp"
#include <string>

void type_text(std::string t, sf::RenderWindow w) {
        sf::Font font;

        font.loadFromFile("consola.ttf");

        sf::Text text(t, font, 12);
        w.draw(text);
        w.display();
}

int main() {
    sf::RenderWindow window(sf::VideoMode(860, 540), "Test");
    sf::Event e;

    while (window.isOpen()) {
        while (window.pollEvent(e)) {
            if (e.type == sf::Event::Closed)
                window.close();
        }
    }
    
    type_text("this is a test", window); // <= THE PROBLEM

    return 0;
}

On the problem line, 'window' is underlined as an error. The error says

sf::RenderWindow::RenderWindow(const sf::RenderWindow &)': attempting to reference a deleted function

Basically, how can I make it so I am able to use window to overload the function and use it?


Solution

  • The window object sf::RenderWindow w cannot be copied and must be passed by a reference sf::RenderWindow& w to the type_text().