Dai*_*vys 1 multithreading boost class object
我正在创建c ++游戏服务器.服务器创建了许多对象monster,每个对象都monster应该具有特定功能的线程.
我收到错误:
error C2064: term does not evaluate to a function taking 0 arguments
thread.hpp(60) : while compiling class template member function 'void
boost::detail::thread_data<F>::run(void)'
Run Code Online (Sandbox Code Playgroud)
monster.cpp:
#include "monster.h"
monster::monster(string temp_mob_name)
{
//New login monster
mob_name = temp_mob_name;
x=rand() % 1000;
y=rand() % 1000;
boost::thread make_thread(&monster::mob_engine);
}
monster::~monster()
{
//Destructor
}
void monster::mob_engine()
{
while(true)
{
Sleep(100);
cout<< "Monster name"<<mob_name<<endl;
}
}
Run Code Online (Sandbox Code Playgroud)
monster.h:
#ifndef _H_MONSTER_
#define _H_MONSTER_
//Additional include dependancies
#include <iostream>
#include <string>
#include "boost/thread.hpp"
using namespace std;
class monster
{
public:
//Functions
monster(string temp_mob_name);
~monster();
//Custom defined functions
void mob_engine();
int x;
int y;
};
//Include protection
#endif
Run Code Online (Sandbox Code Playgroud)
mob_engine是一个非静态成员函数,因此它有一个隐含的这个参数.
试试这个:
boost::thread make_thread(boost::bind(&monster::mob_engine, this));
Run Code Online (Sandbox Code Playgroud)
根据这个类似的问题提升:线程 - 编译器错误你甚至可以通过简单编写来避免使用bind:
boost::thread make_thread(&monster::mob_engine, this);
Run Code Online (Sandbox Code Playgroud)
此外,您可能希望声明一个boost :: thread成员变量来保持对该线程的引用.
| 归档时间: |
|
| 查看次数: |
3112 次 |
| 最近记录: |