简体   繁体   English

C++ 和 SFML,没有重载函数“sf::RenderWindow.draw()”的实例与参数列表匹配

[英]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++.我目前正在尝试使用 C++ 中的 SFML 制作一个简单的乒乓球游戏。 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:这是 ball.h 文件:

#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.任何帮助,将不胜感激。

sf::RenderWindow::draw requires an implementation of sf::Drawable . sf::RenderWindow::draw需要sf::Drawable的实现。 You can either pass sf::CircleShape (it's a sf::Drawable ) directly or implement sf::Drawable and delegate the call.您可以直接传递sf::CircleShape (它是一个sf::Drawable )或实现sf::Drawable并委托调用。

class Ball : public sf::Drawable {
public:
    double x, y;
    int Radius;

    void draw(sf::RenderTarget& target, sf::RenderStates states) const override {
      sf::CircleShape shape(Radius);
      shape.setFillColor(BallColour);
      shape.draw(target, states);
    }
};

You had not call function of render, you just wrote window.draw(b) , and programm dont know what do you mean.你没有调用渲染函数,你只是写了window.draw(b) ,程序不知道你是什么意思。 In Ball.h you should write:在 Ball.h 中你应该写:

 #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:在你的 main.cpp 中,你应该调用你的函数 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:没有匹配的函数调用 &#39;sf::RenderWindow::draw(<unresolved overloaded function type> )&#39;| C++ 中的 SFML - Error: no matching function for call to 'sf::RenderWindow::draw(<unresolved overloaded function type>)'| SFML in C++ 错误:没有匹配的函数调用 &#39;sf::RenderWindow::draw(<unresolved overloaded function type> )&#39; SFML C++ - Error: no matching function for call to 'sf::RenderWindow::draw(<unresolved overloaded function type>)' SFML C++ 错误没有重载函数的实例“getline”匹配参数列表c ++ - error no instance of overloaded function “getline” matches the argument list c++ 没有重载函数的实例与参数列表匹配c ++ - no instance of overloaded function matches the argument list c++ C++ 'getline' 没有重载的实例 function 匹配参数列表 - C++ 'getline' No instance of overloaded function matches argument list 没有重载的实例 function 匹配参数列表 C++ - no instance of overloaded function matches argument list C++ C ++没有重载函数getline的实例匹配参数列表 - C++ No instance of overloaded function getline matches argument list getline在C ++中使用字符串返回错误:没有重载函数getline的实例与参数列表匹配 - getline using strings in C++ returning error: No instance of overloaded function getline matches the argument list 映射中的C ++结构作为值-错误“没​​有重载函数的实例与参数列表匹配” - c++ struct in map as value - error “no instance of overloaded function matches the argument list” C++ 多个重载实例 function 与创建 header 文件时的参数列表匹配 - C++ more than one instance of overloaded function matches the argument list when creating a header file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM