Joe Matt Joe Matt - 23 days ago 6
C++ Question

C++ Error 3867 with templates and multi-threading

I read some discussions on the web and on this site, furthermore I didn't manage to understand my error. I've a particular queue called BlockingQueue with a template:

template <typename T, int size>
class BlockingQueue
{

public:
//constructor and other methods
void inserisci(T val) {
//do work
};

private:
queue <T> fifo;
atomic<boolean> isClosed;
mutex m;
};


In the main, I wrote the following lines:

BlockingQueue<int, 5> bq;
int k=8;
thread t(bq.inserisci, k);
t.join();
return 0;


While compiling, I obtain this error:


Error C3867: 'BlockingQueue::inserisci': function call missing
argument list; use '&BlockingQueue::inserisci' to create a
pointer to member


I read the suggestion of Visual Studio, so I tried to make a function pointer, but it does not work the same.

Answer

The correct code would look like following:

typedef BlockingQueue<int, 5> BQ5;
BQ5 bq;
int k=8;
thread t(std::bind(&BD5::inserisci, &bq, k));
t.join();
return 0;