首页 > 解决方案 > 在 sf::thread 中运行同一个类的成员函数

问题描述

简单而简短:第 4 行有什么问题?我不能用同一类的 0 个参数调用 void。

#include "Game.h"

Game::Game(int width, int height, std::string title) {
    sf::RenderWindow* window = new sf::RenderWindow(sf::VideoMode(width, height), title, sf::Style::Titlebar | sf::Style::Close);

    gameThread = new sf::Thread(&Game::run, window);

    window->setActive(false);
    sf::Event event;

    gameThread->launch();
    while (window->pollEvent(event)) {
        if (event.type = sf::Event::Closed)
            window->close();

        delete &event;
    }
    gameThread->wait();
}


Game::~Game() {
    delete &clock;
    delete &window;
    delete &gameThread;

    clock = nullptr;
    window = nullptr;
    gameThread = nullptr;
}

void Game::run(sf::RenderWindow* window) {
    while (window->isOpen()) {
        window->clear();
        window->display();
    }
}

在 sf::Thread 类的文档中,所有内容都在示例中进行了解释,但并不是我要做什么。Visual Studio 只是说没有合适的默认构造函数可用。

标签: c++multithreadingsfml

解决方案


推荐阅读