我正在尝试使用SDL2_Net创建UPD互联网连接。我想我可能想要在单独的线程上监听数据,所以我尝试创建一个。 我得到的错误是:
1>e:\microsoft visual studio 11.0\vc\include\functional(1152): error C2064: term does not evaluate to a function taking 0 arguments
1> class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments
1> e:\microsoft visual studio 11.0\vc\include\functional(1152) : while compiling class template member function 'void std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>::operator ()(void)'
1> with
1> [
1> _Forced=true,
1> _Ret=void,
1> _Fun=std::_Pmf_wrap<void (__thiscall ListenerUDP::* )(void),void,ListenerUDP,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
1> _V0_t=std::_Nil,
1> _V1_t=std::_Nil,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil
1> ]
1> e:\microsoft visual studio 11.0\vc\include\thr\xthread(195) : see reference to function template instantiation 'void std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>::operator ()(void)' being compiled
1> with
1> [
1> _Forced=true,
1> _Ret=void,
1> _Fun=std::_Pmf_wrap<void (__thiscall ListenerUDP::* )(void),void,ListenerUDP,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
1> _V0_t=std::_Nil,
1> _V1_t=std::_Nil,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil
1> ]
1> e:\microsoft visual studio 11.0\vc\include\thread(52) : see reference to class template instantiation 'std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>' being compiled
1> with
1> [
1> _Forced=true,
1> _Ret=void,
1> _Fun=std::_Pmf_wrap<void (__thiscall ListenerUDP::* )(void),void,ListenerUDP,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
1> _V0_t=std::_Nil,
1> _V1_t=std::_Nil,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil
1> ]
1> c:\users\aleksander\documents\visual studio 2012\projects\ledii - internet\ledii - internet\listenerudp.cpp(8) : see reference to function template instantiation 'std::thread::thread<void(__thiscall ListenerUDP::* )(void)>(_Fn)' being compiled
1> with
1> [
1> _Fn=void (__thiscall ListenerUDP::* )(void)
1> ]
文档说:
void foo()
{
// do stuff...
}
void bar(int x)
{
// do stuff...
}
int main()
{
std::thread first (foo); // spawn new thread that calls foo()
std::thread second (bar,0); // spawn new thread that calls bar(0)
std::cout << "main, foo and bar now execute concurrently...\n";
// synchronize threads:
first.join(); // pauses until first finishes
second.join(); // pauses until second finishes
std::cout << "foo and bar completed.\n";
return 0;
}
这是我的班级标题:
#pragma once
#include <SDL_net.h>
#include <thread>
#include <vector>
#include <string>
using namespace std;
class ListenerUDP {
private:
thread *threadListen;
bool threadExit;
vector <string> events;
void threadLoop();
public:
ListenerUDP();
~ListenerUDP();
};
我的班级来源:
#include "ListenerUDP.h"
ListenerUDP::ListenerUDP() {
printf("Connecting...\n");
threadExit = false;
threadListen = new thread(&ListenerUDP::threadLoop);
}
ListenerUDP::~ListenerUDP() {
printf("Disconnecting...\n");
threadExit = true;
threadListen->join();
}
void ListenerUDP::threadLoop() {
int i = 0;
while (i != -1 && !threadExit) {
if (i++ >= 1000) {
i = 0;
printf("Thread simulated tick!\n");
}
}
}
答案 0 :(得分:0)
当将类成员函数传递给std :: thread时,它需要一个类实例。所以你可以将 this 作为参数传递给std :: thread构造函数。
threadListen = new thread(&ListenerUDP::threadLoop, this);