Shrey
Shrey

Reputation: 98

Visual Studio 2015 “non-standard syntax; use '&' to create pointer for member”

While creating thread using CreateThread() in Win32 Application , I am getting this error . In CreateThread(NULL,0,pSample->Resize(),NULL,0,NULL); It shows error in function calling .

I do have several files as :

Main.cpp

WinMain()
 {
   //Create sample
    return Win32Appliaction::Run(&sample);
  }

Win32Application.cpp

int Win32Application::Run(DXSample* pSample)
  {
     //Create Window
     pSample->Init();
     pSample->Render();
     CreateThread(NULL,0,pSample->Resize,NULL,0,NULL);//error occurs
     pSample->Destroy();
  }

DXSample.h

class DXSample
   {
     public:
           virtual void Init() =0; //and rest all functions
   };

HelloTexture.h

 class HelloTexture:public DXSample
   {
       public : 
             virtual void Init();//all other functions similarly
    }

HelloTexture.cpp

void Hellotexture::Init()
 { //code
  } 
 void Hellotexture::Resize()
 {
    //code 
  }

Upvotes: 1

Views: 1724

Answers (2)

dodo951
dodo951

Reputation: 454

Parameter #2 of the CreateThread must be a pointer to function matching ThreadProc signature. You can not pass result of pSample->Resize() (which is void) or a pointer to Resize function itself (because this is a non-static class member function). Also you may want to use ::std::thread instead of calling WinApi directly.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409354

You are calling the pSample->Resize() function, passing the returned value to the CreateFunction as the function to run.

If you want the Resize function to be run in its own thread there are two things you need to do:

  1. Pass a pointer to the function instead, e.g. &HelloTexture::Resize.
  2. Make the function static.

The first point is about passing a pointer to the function itself to CreateThread, who will create the thread and call the function from the new thread.

The second point is because non-static member function needs an object, an instance of the class to be called on.


There are also another problem with the code you have. Remember that threads are running parallel to each other. If you create a thread it will run independently from other threads. That means the object you use, and all its data, must be kept alive until the end of the thread.

I'm bringing this up because you seemingly want to create the thread, and then immediately call pSample->Destroy() which is like pulling the rug out from under some other persons feet.

Do you really want to create a thread here?

Upvotes: 0

Related Questions