error C2064: term does not evaluate to a function

2019-07-26 01:16发布

问题:

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

I get error :

 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:

#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

回答1:

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

Try this:

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

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

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