簡體   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