Reputation: 118
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,
Upvotes: 2
Views: 652
Reputation: 118
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);
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.
Upvotes: 0
Reputation: 3142
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);
Upvotes: 2