1

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.

masrtis
  • 1,282
  • 17
  • 32
Songy
  • 851
  • 4
  • 17
  • The compiler is pretty clear: `use '&XboxControler::VibreateThread'`, isn't it? For more information and samples have a look at the [`std::thread` documentation](http://en.cppreference.com/w/cpp/thread/thread). Also note you can't use a non static class member function that way to initialize a thread. – πάντα ῥεῖ Mar 21 '14 at 17:18
  • @πάνταῥεῖ I've tried that and it says term does not evaluate to a function taking 1 argument so though that it might be something else. – Songy Mar 21 '14 at 17:21
  • @Songy: Indeed. Since it's a member function, you'll also need an `XboxController` (such as `this`) to call it on. – Mike Seymour Mar 21 '14 at 17:31

1 Answers1

0

I have never tried using std::thread but from thread reference -

4) The copy constructor is deleted; threads are not copyable. No two std::thread objects may represent the same thread of execution.

Your code invokes copy constructor.

std::thread vibThread = std::thread(XboxControler::VibreateThread,
                                    vibStruct(leftVal, rightVal, 1000));

Also, you are trying to bind a rvalue to a non-const reference. So try -

std::thread vibThread(&XboxControler::VibreateThread,
                      vibStruct(leftVal, rightVal, 1000));

                     // Note the addition of & operator as compiler suggested

And change -

void XboxControler::VibreateThread(const vibStruct& vals);
                                 //^^^^ notice the const key word
Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • 1
    Also `XboxControler::VibreateThread` needs to be a static class member in this case. – πάντα ῥεῖ Mar 21 '14 at 17:23
  • @πάνταῥεῖ You are correct. But it seems OP is using member variables in the member function (`_controllerNum`). I think OP has to create an object and pass the parameter like `(obj.*VibreateThread)`. – Mahesh Mar 21 '14 at 17:25