2014-01-09 117 views
0

我有一个Foo类,它有一个主函数和执行函数。我想用execute函数启动未知数量的线程,但是当我尝试编译代码时,我总是得到error C2064: term does not evaluate to a function taking 1 arguments多线程 - 类中的异步线程

foo.h中

#ifndef BOT_H 
#define BOT_H 

#pragma once 
#include <WinSock2.h> 
#include <WS2tcpip.h> 
#include <string> 
class foo 
{ 
public: 
    foo(char *_server, char *_port); 
    ~foo(void); 
private: 
    char *server; 
    char *port; 

    void execute(char *cmd); 
    void main(); 
}; 

#endif 

foo.c的

#include <thread> 
#include "bot.h" 
#include "definitions.h" 

using namespace std; 
foo::foo(char *_server, char *_port){ 
     ... 
} 

bot::~bot(void) { 
     ... 
} 
void bot::execute(char *command){ 
    ... 
} 
    void bot::main(){ 
     thread(&bot::execute, (char*)commanda.c_str()).detach(); 
    } 

我应该如何创建类的成员函数的线程?

感谢您的任何答案

回答

5

你需要一个bot对象调用的成员函数:

thread(&bot::execute, this, (char*)commanda.c_str()) 
         ^^^^ 

虽然你真的应该要么改变采取任何std::stringconst char*功能。如果这个函数试图修改字符串,或者线程仍在使用它,则会导致commanda被破坏。

lambda可能更具可读性;并且还通过捕获字符串的副本来修复终身惨败:

thread([=]{execute((char*)commanda.c_str();})