C++;和SFML,没有重载函数的实例;sf::RenderWindow.draw();匹配参数列表 我现在正试图用C++中的SFML来制作一个简单的乒乓游戏。我总是在主文件中得到错误 #包括 #包括“Ball.h” 球b; int main() { //创建窗口 sf::RenderWindow(sf::VideoMode(1200600),“我的窗口”); //只要窗口打开,就运行程序 while(window.isOpen()) { //检查自上次循环迭代以来触发的所有窗口事件 sf::事件; while(window.pollEvent(事件)) { //“请求关闭”事件:我们关闭窗口 如果(event.type==sf::event::Closed) window.close(); } //用黑色清除窗户 窗口。清除(sf::颜色::黑色); //把一切都画在这里。。。 窗口。绘制(b); //结束当前帧 window.display(); } 返回0; }

C++;和SFML,没有重载函数的实例;sf::RenderWindow.draw();匹配参数列表 我现在正试图用C++中的SFML来制作一个简单的乒乓游戏。我总是在主文件中得到错误 #包括 #包括“Ball.h” 球b; int main() { //创建窗口 sf::RenderWindow(sf::VideoMode(1200600),“我的窗口”); //只要窗口打开,就运行程序 while(window.isOpen()) { //检查自上次循环迭代以来触发的所有窗口事件 sf::事件; while(window.pollEvent(事件)) { //“请求关闭”事件:我们关闭窗口 如果(event.type==sf::event::Closed) window.close(); } //用黑色清除窗户 窗口。清除(sf::颜色::黑色); //把一切都画在这里。。。 窗口。绘制(b); //结束当前帧 window.display(); } 返回0; },c++,sfml,C++,Sfml,这是ball.h文件: #pragma一次 #包括 #包括 #包括 班级舞会 { 公众: 双x,y; 整数半径; void渲染(int-Rad,sf::Color-BallColour){ sf::圆形形状(Rad); //将形状颜色设置为绿色 形状。setFillColor(BallColour); } }; 我不确定发生错误的原因。任何帮助都将不胜感激。sf::RenderWindow::draw需要实现sf::Drawable。您可以直接传递sf::CircleShape(它是一个sf:

这是ball.h文件:

#pragma一次
#包括
#包括
#包括
班级舞会
{
公众:
双x,y;
整数半径;
void渲染(int-Rad,sf::Color-BallColour){
sf::圆形形状(Rad);
//将形状颜色设置为绿色
形状。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);
    }
};

您并没有调用render函数,只编写了
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
        }
    };
#pragma一次
#包括
#包括
#包括
班级舞会
{
公众:
双x,y;
整数半径;
void渲染(int-Rad、RenderWindow和window){
sf::圆形形状(Rad);
//将形状颜色设置为绿色
setFillColor(sf::Color::Green);
shape.setPosition(600300);//球的设置位置
window.draw(shape);//若你们知道变量的引用是什么,你们就会明白它是什么
}
};
在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;
}
#包括
#包括“Ball.h”
int main()
{
//创建窗口
sf::RenderWindow(sf::VideoMode(1200600),“我的窗口”);
Ball b;//我建议您在main函数中创建类的对象
//只要窗口打开,就运行程序
while(window.isOpen())
{
//检查自上次循环迭代以来触发的所有窗口事件
sf::事件;
while(window.pollEvent(事件))
{
//“请求关闭”事件:我们关闭窗口
如果(event.type==sf::event::Closed)
window.close();
}
//用黑色清除窗户
窗口。清除(sf::颜色::黑色);
b、 Render(10,window);//给出window上的函数引用
//结束当前帧
window.display();
}
返回0;
}

您没有设置球的位置,希望窗口绘制球?在学习SFML之前,您应该了解面向对象编程的基础知识、指针和参考谢谢您的帮助,不幸的是,在修改代码后,程序仍然给我错误:(记住我是一个C++初学者,没有任何错误,你只是反转了依赖关系。OP想要渲染<代码>球,所以你必须实现这个接口。<代码>球<代码>不应该知道代码> ReNeldWindows < /C>或任何其他代码> IrnDealEdvest[/Cord]。对于这个问题,您的代码是good@fdan问题的作者试图只画类的对象:
window.draw(b)
,但这并没有打扰你们吗?