0

I've got a class Foo, which have a main function and execute function. I want to start an unknown number of threads with the execute function, but when I try to compile the code I always get 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();
    }

How should I create threads from class member functions?

Thanks for any answer

1 Answer 1

5

You need a bot object to call the member function on:

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

although you really should either change the function to take either std::string or const char*. You have a minefield of undefined behaviour here, if either the function tries to modify the string, or commanda is destroyed while the thread is still using it.

A lambda may be more readable; and would also fix the lifetime fiasco by capturing a copy of the string:

thread([=]{execute((char*)commanda.c_str();})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.