winapivisual-c++createthread

error C3867 when calling class function with CreateThread


I try to pass a class function to CreateThread called from main function, I got error :

error C3867: 'Display::fill_matrix': function call missing argument list; use '&Display::fill_matrix' to create a pointer to member


class Display
{
public:
    Display();
    DWORD WINAPI fill_matrix();
};

Display display;
main() {
    CreateThread(NULL, 0, display.fill_matrix, NULL, 0, 0);
}

Solution

  • fill_matrix() is a non-static member function. Therefore its first argument is a pointer to a Display class instance. That is what the compiler complains about. The normal way to solve this is make a static member function and pass that to CreateThread. Here is what it would like :

    class Display
    {
    public:
        Display();
    
        static DWORD WINAPI fill_matrix_static(void* obj_ptr) {
            Display* display_ptr = (Display*) obj_ptr;
            return display_ptr->fill_matrix();
        }
    
        DWORD WINAPI fill_matrix();
    };
    

    and then call it like this :

    CreateThread(NULL, 0, fill_matrix_static, (void*) &display, 0, 0);