2

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,

12
  • Did you intentionaly cut off ctor body? Commented Oct 30, 2015 at 12:06
  • What version of MSVS are you using? What is the signature of DeviceHandler::readFromBus? Commented Oct 30, 2015 at 12:07
  • @NathanOliver He's passing _handle... Commented Oct 30, 2015 at 12:09
  • @MatthäusBrandl Oops so he is. removed that bit. Commented Oct 30, 2015 at 12:11
  • 1
    Is it possible that DeviceHandler::readFromBus is an overloaded function? See this answer Commented Oct 30, 2015 at 12:18

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

8 Comments

Nice workaround ! It actually does compile, but throws syntax error onto readFromBus prototype now.. "Error C2059 syntax error : 'constant' "
If the syntax were wrong it would not compile, I don't understand. I created a similar situation as yours where the cast works.
I guess our two environnement differs, i'm actually launching a thread to a member member function, whereas in your case, its actually a basic func that calls thread. You actually use bind also, and i'm not aware of its behaviour and the usage it implies.. i'll check reference cheat sheets. furthermore, i can't see any constant declaration on the pure prototype, do you ?
Correct, I made my example minimal. If you had given a minimal example I could have reused your code. I don't get the "constant declaration on the pure prototype" part. And I still can't understand how your code compiles with syntax errors in it. Please try to explain more precisely.
My example now starts threads. What it lacks is a templated pure virtual method as this is illegal in C++. I can't understand how this possibly works for you. My best guess is that your class is templated but that is not what you posted. I can't help you without the MCVE.
|
0

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.

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.