Thread: Strange type incompatibility using function pointers

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    903

    Strange type incompatibility using function pointers

    Hey guys/girls ! I'm programming an application for which I have built classes ThreadedWorker and ThreadedWorkerManager. The ThreadedWorkerManager class needs to have a pointer to the callback function that will be used by the CreateThread() function. To lighten up a little bit the code, I have typedef'ed the function pointer and named it THREAD_PROC. The constructor of my ThreadedWorkerManager class takes a THREAD_PROC as only argument and later uses it. However, when I instantiate it, I get the following error:

    Code:
    'ThreadedWorkerManager<T>::ThreadedWorkerManager(THREAD_FUNC)' : cannot convert parameter 1 from 'DWORD (void *)' to 'THREAD_FUNC'
            with
            [
                T=WorkData *
            ]
    I don't quite get why it complains, here's part of the code for ThreadedWorkerManager:
    Code:
    typedef DWORD (*THREAD_FUNC)(void*);
    
    template <typename T> class ThreadedWorkerManager;
    template <typename T> class ThreadedWorker;
    
    template <typename T>
    class ThreadedWorkerManager {
    public:
    	ThreadedWorkerManager(THREAD_FUNC ptr = 0) : WorkerProc(ptr) {
    		// ...
    	}
    
    	// ...
    private:
    	THREAD_FUNC WorkerProc;
    	//...
    };
    And this, is where the error occurs:
    Code:
    DWORD WINAPI ThreadProc(void* data) {
    	// ...
    }
    
    SettingsManager::SettingsManager() {
    	// Bang ! I'm dead !
    	TWMgr = new ThreadedWorkerManager<WorkData*>(ThreadProc);
    	// ...
    }
    Do you guys have any idea of what could cause this error ? My friend and I are stumped and can't get how this is wrong.

    Thanks.

    PS: I'm using MSVC++ 7.1

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    That WINAPI changes it (__stdcall, default is __cdecl).

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Your calling conventions don't match. The typedef needs to be:
    Code:
    typedef DWORD (WINAPI *THREAD_FUNC)(void*);

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Thanks. I fixed the other dozen of errors and now I'm ready to test. Lots of fun predicted -.-

    Ahah, thanks again mates.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM