another "does not match ‘void* (*)(void*)’" error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jianwee
    New Member
    • Feb 2010
    • 7

    another "does not match ‘void* (*)(void*)’" error

    Hi all,

    I have encountered the above said error for some time. I have researched for possible solutions and have discovered that the error means that the function that I am calling as a thread is a member function. I have already declared the function as a global function, but yet the error still appears. Can anyone help me out here? The code is below. Thx in advance.

    Code:
    #include <pthread.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    bool orderStop=true;
    bool firstTime=true;
    bool stopRobo=false;
    
    void *detectKey(void *arg)
    {
        int y;
        std::cin >> y;
        while (y != 1)
        {
            orderStop = true;
    	stopRobo = true;
        }
        return NULL;
        pthread_exit(0);
    }
    
    void *detectKey(void *arg);
    
    void
    MainLoop::run()
    {
    		pthread_t wait;
    		int rc;
       .....
        while ( isActive() )
        {
            try 
            {
                ....
    		if (orderStop)
    		{
    			....
    			// create thread to wait for keyboard input
    			rc = pthread_create(&wait,NULL,detectKey,NULL);
    		};
    		....
    		orderStop = false;
    		std::cout <<"\nBoolean orderStop is set at: " << orderStop <<endl;
    		
    		}
           catch(....){....}
         }
    }
  • grayMist
    New Member
    • Feb 2010
    • 12

    #2
    cant see any issues in the code as such.
    Which compiler are you using?
    Can you post the exact error messages from the compiler here along with the concerned code blocks ?

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Please post the entire error message which normally gives the type of the object supplied and the type that the function was expecting.

      Comment

      • Jianwee
        New Member
        • Feb 2010
        • 7

        #4
        Thx for the reply so far. Here's some info that might help:
        I'm running on Ubuntu 9.04.
        GCC 4.1.3
        G++ 4.1.3

        The whole error message is:
        error: arguement of type 'void (localnav::Main Loop::)(void*)' does not match 'void* (*)(void*)'

        As I am working on ORCA (open source code for robotics), the whole code is too long to be displayed. The code where the error is pointing to is at the pthread_create method in the previous code block.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          The prototype I found for pthread_create is
          Code:
          int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
              void *(*start_routine)(void*), void *arg);
          You pass detectKey as the start_routine argument. Its prototype is
          Code:
          void *detectKey(void *arg);
          I don't see any incompatibility . I don't know why you get an error.

          The double-colons in the error message make me think you might be using C++. I'm not aware of any reason C++ would be more persnickety about function pointers than C.

          I suggest you take a look inside <pthread.h> and see if your pthread_create has a different prototype than the one I found on the internet.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Does you MainLoop class have a detectKey member function?

            You can not pass member functions to functions like pthread_create that require a function pointer unless they are static because a member function has to be called on an object which pthread_create can't do.

            If you have an externally declared detectKey function, as your code suggests as well as a detectKey member function in the MainLoop class then you need to make sure you address the correct function using the scope resolution operator (::) like so

            rc = pthread_create( &wait,NULL,::detectKey,NULL) ;

            Comment

            • Jianwee
              New Member
              • Feb 2010
              • 7

              #7
              To Banfa: I have already declared the detectKey function at the very beginning of the mainloop.cpp, right below the include, so that should be a global function right? But I will try what you have suggested. Sorry for getting it wrong, I'm only starting out on C++ recently.

              To donbock: I will take a look at <pthread.h> as you suggested. Thanks.

              Comment

              • Jianwee
                New Member
                • Feb 2010
                • 7

                #8
                Banfa,

                Thx a lot! Your method works!

                Comment

                Working...