theColonel26
Posts: 39
Joined: Tue Jun 18, 2013 3:04 pm

C++11 <thread>: no matching function for call

Thu Jul 18, 2013 4:20 pm

I'm getting this on build
no matching function for call to 'std::thread::thread(<unresolved overloaded function type>)'
for the line

Code: Select all

thread thread1(run);
here is the the class

Code: Select all

class Blinker
{
	private: 
		volatile bool boolStop;

		bool step[6];

		void run()
		{
			timer t1;
			t1.setInterval(4);
			for(;;)
			{
				if(t1.elapsed() < .5 && !step[0])
				{
					digitalWrite(200, 1);
					step[0] = true;
				}
				else if (t1.elapsed() < 1.0  && !step[1])
				{
					digitalWrite(201, 1);
					step[1] = true;
				}
				else if (t1.elapsed() < 1.5  && !step[2])
				{
					digitalWrite(200, 0);
					step[2] = true;

				}
				else if (t1.elapsed() < 2.0  && !step[3])
				{
					digitalWrite(201, 0);
					step[3] = true;
				}
				else if (t1.elapsed() < 2.5  && !step[4])
				{
					digitalWrite(200, 1);
					digitalWrite(201, 1);
					step[4] = true;
				}
				else if (t1.elapsed() < 3.0  && !step[5])
				{
					digitalWrite(200, 0);
					digitalWrite(201, 0);
					step[5] = true;
				}
				else if (t1.elapsed() > 4.0)
				{
					for(int i = 0; i < sizeof(step); i++)
					{
						step[i] = 0;
					}
					t1.reset();
				}

				
				if (this->boolStop)
				{
					break;
				}
			}
		}

	public:
		Blinker()
		{
			boolStop = false;
		}

		void start()
		{
			thread thread1(run);

		}

		void stop()
		{
			boolStop = true;
		}

};

User avatar
elektrknight
Posts: 140
Joined: Sat Mar 02, 2013 1:25 pm

Re: C++11 <thread>: no matching function for call

Thu Jul 18, 2013 4:56 pm

What are the includes and what compiler are you using?
Placek Malinowy to jest to!

theColonel26
Posts: 39
Joined: Tue Jun 18, 2013 3:04 pm

Re: C++11 <thread>: no matching function for call

Thu Jul 18, 2013 5:30 pm

Code: Select all

extern "C" { 
#include <wiringPi.h>
#include <piFace.h>
}
#include <iostream>
#include <ctime>
#include <thread>
I'm using the G++ 4.6

User avatar
AndyD
Posts: 2320
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: C++11 <thread>: no matching function for call

Thu Jul 18, 2013 10:13 pm

A class member function needs to be bound to a class object:
theColonel26 wrote:

Code: Select all

		void start()
		{
			thread thread1(run);

		}
To create a thread using a class member function you need to pass the "this" pointer as the second parameter to the std::thread constructor:

Code: Select all

		void start()
		{
			thread thread1(run, this);

		}

theColonel26
Posts: 39
Joined: Tue Jun 18, 2013 3:04 pm

Re: C++11 <thread>: no matching function for call

Fri Jul 19, 2013 1:16 pm

Ok I tried your suggestion, but I'm still getting the same error. :(

User avatar
AndyD
Posts: 2320
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: C++11 <thread>: no matching function for call

Fri Jul 19, 2013 10:24 pm

Here is a cut down version that compiles

Code: Select all

#include <iostream>
#include <ctime>
#include <thread>

using namespace std;

class Blinker
{
   private:
      volatile bool boolStop;

      void run()
      {
      }

   public:
      Blinker() : boolStop(false)
      {
      }

      void start()
      {
         thread thread1(&Blinker::run, this);
      }

      void stop()
      {
         boolStop = true;
      }

};

int main(void)
{
    Blinker blinker;

    blinker.start();

    while(1)
    {
        // do nothing
    }

    return 0;
}
compiled using

Code: Select all

g++ -std=c++0x -pthread blinker.cxx -o blinker
Edit: This now compiles, but what do you expect the code to do when thread1 is destroyed at the end of start()? You will need to call thread1.detach() in the start() method.

theColonel26
Posts: 39
Joined: Tue Jun 18, 2013 3:04 pm

Re: C++11 <thread>: no matching function for call

Tue Jul 23, 2013 1:40 pm

thanks, I got it to compile finally.

this is what I needed

Code: Select all

thread1 = thread(&Blinker::run, this);
I was using this which didn't work.

Code: Select all

thread1 = thread(run, this);
what is the difference though? I assume the top one must be passing a reference to the function. So the bottom one is pass by copy syntax. which probably isn't possible for a function right?

User avatar
AndyD
Posts: 2320
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: C++11 <thread>: no matching function for call

Tue Jul 23, 2013 11:42 pm

theColonel26 wrote:what is the difference though? I assume the top one must be passing a reference to the function.
The top one is passing a pointer to a member function (there are no function references in C++ only pointer to functions). The difference is that in C++ to take the address of a member function run in class Blinker the syntax is &Blinker::run. For non-member functions C++ uses the same syntax as C. In C (lets say you had a function void stop(void);) &stop would give you the address of function stop, however you can drop the ampersand (&) and just use stop.
theColonel26 wrote:So the bottom one is pass by copy syntax. which probably isn't possible for a function right?
No, this isn't about pass-by-value vs pass-by-reference. C++ doesn't have function references only function pointers. A pointer to member function is different to a pointer to non-member function as you need to specify the class and the function (and use an ampersand) when taking the address.

I hope that helps.

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 6 guests