Search code examples
c++sfml

C++ and SFML, no instance of the overloaded function "sf::RenderWindow.draw()" matches the argument list


I am currently trying to make a simple pong game with SFML in C++. I consistently get the error in the main file.

#include <iostream>
#include "Ball.h"

Ball b;

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(1200, 600), "My window");

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
            window.close();
        }

        // clear the window with black color
        window.clear(sf::Color::Black);

        // draw everything here...
        window.draw(b);

        // end the current frame
        window.display();
    }

    return 0;
}

This is the ball.h file:

#pragma once
#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
class Ball
{
public:
    double x, y;
    int Radius;

    void Render(int Rad, sf::Color BallColour) {
        sf::CircleShape shape(Rad);
        // set the shape color to green
        shape.setFillColor(BallColour);
    }
};

I am unsure as to why the error occurs. Any help would be appreciated.


Solution

  • You had not call function of render, you just wrote window.draw(b), and programm dont know what do you mean. In Ball.h you should write:

     #pragma once
        #include <iostream>
        #include <SFML/Window.hpp>
        #include <SFML/Graphics.hpp>
        class Ball
        {
        public:
            double x, y;
            int Radius;
        
            void Render(int Rad, RenderWindow& window) {
                sf::CircleShape shape(Rad);
                // set the shape color to green
                shape.setFillColor(sf::Color::Green);
                shape.setPosition(600, 300);//SETTING POSITION OF THE BALL
                window.draw(shape);//if you know what is reference on variable, you will understand what is it
            }
        };
    

    And in your main.cpp you shhould call your function Render:

    #include <iostream>
    #include "Ball.h"
    
    int main()
    {
        // create the window
        sf::RenderWindow window(sf::VideoMode(1200, 600), "My window");
    
        Ball b; //I advice you to create object of class in main function
    
        // run the program as long as the window is open
        while (window.isOpen())
        {
            // check all the window's events that were triggered since the last iteration of the loop
            sf::Event event;
            while (window.pollEvent(event))
            {
                // "close requested" event: we close the window
                if (event.type == sf::Event::Closed)
                window.close();
            }
    
            // clear the window with black color
            window.clear(sf::Color::Black);
    
            b.Render(10, window);// give the function reference on window
    
            // end the current frame
            window.display();
        }
    
        return 0;
    }