繁体   English   中英

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

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

我目前正在尝试使用 C++ 中的 SFML 制作一个简单的乒乓球游戏。 我一直在主文件中收到错误。

#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;
}

这是 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);
    }
};

我不确定为什么会发生错误。 任何帮助,将不胜感激。

sf::RenderWindow::draw需要sf::Drawable的实现。 您可以直接传递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);
    }
};

你没有调用渲染函数,你只是写了window.draw(b) ,程序不知道你是什么意思。 在 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
        }
    };

在你的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM