I'm having a bit of trouble creating a thread in C++. When I attempt to create one I get these two errors...
error C2661: 'std::thread::thread' : no overloaded function takes 2 arguments
error C3867: 'XboxControler::VibreateThread': function call missing argument list; use '&XboxControler::VibreateThread' to create a pointer to member
I've searched the internet but to no avail, I'm not too sure what's causing it and was hoping someone out there can point out where I'm going wrong.
The relevant code to the problem is...
This currently is using 1000 for milliseconds for testing purposes. It creates the thread for a controller vibration so it can last a certain amount of time before stopping.
void XboxControler::Vibrate(int leftVal, int rightVal)
{
std::thread vibThread = std::thread(XboxControler::VibreateThread,vibStruct(leftVal, rightVal, 1000));
}
This is the vibration thread, it starts and stops the controller vibration.
void XboxControler::VibreateThread(vibStruct& vals){
XINPUT_VIBRATION Viberation;
ZeroMemory(&Viberation, sizeof(XINPUT_VIBRATION));
Viberation.wLeftMotorSpeed = vals.leftVal;
Viberation.wRightMotorSpeed = vals.rightVal;
XInputSetState(_controllerNum, &Viberation);
Sleep(vals.milliseconds);
Viberation.wLeftMotorSpeed = 0;
Viberation.wRightMotorSpeed = 0;
XInputSetState(_controllerNum, &Viberation);
}
This is the struct used in the parameter of the thread, there didn't used to be this and it was only added in an attempt to get the thread running properly. It is declared privately in the header file of the this controller class.
struct vibStruct{
int leftVal;
int rightVal;
int milliseconds;
vibStruct(int leftVal, int rightVal, int milliseconds){
vibStruct::leftVal = leftVal;
vibStruct::rightVal = rightVal;
vibStruct::milliseconds = milliseconds;
}
};
Hope that someone can help, I'm sure that there might be a better way of getting the vibration to last a specific time which if you know I would like to hear but for future reference I would also like to know how to solve this specific problem.
EDIT 1:
Thanks for the help everyone, I read through everything said to create the following that works...
I added the & like was pointed out by the complier which I dismissed at first as it still gave me errors. I created a new thread that wouldn't be destroyed once the Vibrate method executes.
void XboxController::Vibrate(int leftVal, int rightVal)
{
new std::thread(&XboxController::ViberateThread, vibStruct(leftVal, rightVal, 1000), _controllerNum);
}
I made the ViberateThread static as suggested and passed in the controller number. I also made the vals a const as way suggested.
void XboxController::ViberateThread(const vibStruct& vals, int controllerNum){
//code...
}
Thanks for the help everyone, I hope my newly amalgamated code isn't breaking any coding practices or making any people weep at some horrible thing I have done :) P.S. I also noticed the spelling mistakes which have now been fixed so everyone can rest easy.