简体   繁体   中英

std::thread initialization constructor gives compile error

i'm working into a Visual Studio project (v120 Compiler) that uses std::thread to read from usb device aside from the GUI, and the function throws an error : "Error C2661 'std::thread::thread' : no overloaded function takes 3 arguments"

here's code:

class IOThread
{
public:
IOThread(DeviceHandler *handle) : _handle(handle)
~IOThread();

std::thread *getThread(ThreadType type);

template <typename T>
void execRead(std::list<T> *dataStack)
{
    std::thread *thread = getThread(Read);

    if (thread == NULL)
    {
        thread = new std::thread(&DeviceHandler::readFromBus, _handle, dataStack);
        _threadPool.push_back(std::make_pair(Read, thread));
    }
}

private:
DeviceHandler                                       *_handle;
std::vector<std::pair<ThreadType, std::thread *>>   _threadPool;
};

moreover, DeviceHandler is an abstraction class, which defines pure virtual readFromBus function, which prototype is the following

template <typename T>
void readFromBus(std::list<T> *dataStack) = 0;

I wish you not to have the same headache as i do while solving this mess... Regards,

As explained in the comments your situation is the same is in this question . Because the method DeviceHandler::readFromBus() is templated arbitrarily many overloads can be generated. (They share the name but have different signatures).

Because of this the compiler cannot select the correct overload, hence the error message. You will need to tell the compiler which overload to use by a cast. (as this answer explains)

The following cast should do:

thread = new std::thread(
     static_cast<void (DeviceHandler::*)(std::list<T> *)>(&DeviceHandler::readFromBus),
     _handle, dataStack);

I tried to give a MVCE of the error, but i can't test if it compiles; but here's the actual structure of classes using your cast

thread = new std::thread(
 static_cast<void (DeviceHandler::*)(std::list<T> *)>(&DeviceHandler::readFromBus),
 _handle, dataStack);

http://ideone.com/gVh1Du

EDIT: I solved the problem, the issue was the templated pure definition, which i replaced by a function that takes in parameters an abstract struct as follows

typedef struct s_dataStack
{
  DataType type;
  std::list<void *> stack;
} t_dataStack;

and then i cast any stack element with provided type in enum "datatype". Thanks for the help given anyway, it led me to the origins of the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM