【问题标题】:SFML Thread Initialization not WorkingSFML 线程初始化不起作用
【发布时间】:2014-06-20 18:46:53
【问题描述】:

我正在尝试创建一个线程,但是当我编译时,出现以下错误:

1>------ Build started: Project: GameServer, Configuration: Release Win32 ------
1>  Heatbeat.cpp
1>C:\Users\Will\Documents\OpenGL\include\SFML/System/Thread.inl(48): error C2064: term does not evaluate to a function taking 1 arguments
1>          C:\Users\Will\Documents\OpenGL\include\SFML/System/Thread.inl(48) : while compiling class template member function 'void sf::priv::ThreadFunctorWithArg<F,A>::run(void)'
1>          with
1>          [
1>              F=void (__thiscall Heartbeat::* )(sf::IpAddress),
1>              A=sf::IpAddress
1>          ]
1>          C:\Users\Will\Documents\OpenGL\include\SFML/System/Thread.inl(79) : see reference to class template instantiation 'sf::priv::ThreadFunctorWithArg<F,A>' being compiled
1>          with
1>          [
1>              F=void (__thiscall Heartbeat::* )(sf::IpAddress),
1>              A=sf::IpAddress
1>          ]
1>          Heatbeat.cpp(26) : see reference to function template instantiation 'sf::Thread::Thread<void(__thiscall Heartbeat::* )(sf::IpAddress),sf::IpAddress>(F,A)' being compiled
1>          with
1>          [
1>              F=void (__thiscall Heartbeat::* )(sf::IpAddress),
1>              A=sf::IpAddress
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我试图让线程接受一个带有一个参数的函数,但是会产生这个错误。这是我的文件:

void Heartbeat::prepareHeartbeat(ClientHandler clients)
{
    std::vector<sf::IpAddress> ips;


    for(int i = 0; i < clients.size(); i++)
    {
        PlayerSession player = clients.getPlayers().at(i);
        sf::IpAddress ip = player.getIp();
        ips.push_back(player.getIp());
        std::cout << player.getIp() << std::endl;
        sf::Thread thread(&Heartbeat::heartbeat, ip);

        thread.launch();

    }
}

有什么建议吗?

更新:我试过了

sf::Thread thread(&Heartbeat::heartbeat, this, ip);

同样,但这会返回以下错误:

sf::Thread thread(&Heartbeat::heartbeat, this, ip);

【问题讨论】:

  • heartbeat 甚至没有一个参数。它需要一个对象来操作,因为它是一个成员函数。我不了解 SFML,但在 C++(11) 中,它很简单,thread(&amp;Heartbeat::heartbeat, this, ip) 假设您的意思是使用当前对象。
  • 当我将行更改为该行时,我收到此错误:1>Heatbeat.cpp(26): error C2660: 'sf::Thread::Thread' : function does not take 3 arguments
  • 那么我猜 SFML 的工作方式与标准的不同。我不是 SFML 文档,所以我不能告诉你 SFML 做了什么来允许传递数据。
  • 尝试使用 C++11 中引入的 stdlib 线程库。 `#include ´ 在你需要的文件中。我认为使用与 SFML 不同的线程 API 没有问题……至少我使用它而不是 Allegro 的线程库,我觉得它既麻烦又不灵活。
  • 我想继续使用 SFML 线程库,因为它比 C++ 11 线程更便携。但是,如果我无法解决此问题,那么我将切换。

标签: c++ multithreading sfml


【解决方案1】:

有一个非常好的SFML线程教程here。 请仔细查看“常见错误”部分。

简单地说,语法不允许您对 SFML 的线程做您想做的事情。没有构造函数需要三个参数。

您将需要 std::bind 您的函数和参数,或创建一个仿函数或使您的心跳方法静态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 2023-03-14
    • 1970-01-01
    • 2021-06-17
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多