本文介绍了错误C2064:term不评估为一个函数带有0个参数thread.hpp(60)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建c ++游戏服务器。服务器创建许多对象 monster ,每个 monster 应该有其具有特定功能的线程。

I'm creating c++ game server. The server creates many objects monster, and every monster should have its thread with specific function.

我收到错误:

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

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

monster.h

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


推荐答案

mob_engine是一个非静态成员函数,因此它具有隐含的参数。

mob_engine is a non-static member function, so it has an implicit this argument.

/ p>

Try this:

boost::thread make_thread(boost::bind(&monster::mob_engine, this));

根据此类似问题你甚至可以避免使用 bind

According to this similar question boost:thread - compiler error you can even avoid using bind by simply writing:

boost::thread make_thread(&monster::mob_engine, this);

此外,你可能想声明一个boost :: thread成员变量来保持引用线程。

Also, you will probably want to declare a boost::thread member variable to keep a reference to the thread.

这篇关于错误C2064:term不评估为一个函数带有0个参数thread.hpp(60)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 02:41