SFML线程初始化不起作用

时间:2014-06-20 18:46:54

标签: c++ multithreading sfml

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

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);

1 个答案:

答案 0 :(得分:1)

有一个非常好的SFML线程教程here。 请仔细查看&#34;常见错误&#34;。

简单地说,语法不允许您使用SFML的线程执行您想要执行的操作。没有构造函数需要三个参数。

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