I know that in order to create a thread of a method on an Object I can do it in this way:
#include <thread>
using namespace std;
class Character
{
public:
void myFunction(int a){ /* */ }
void startThreadMyFunction(int a){
thread Mf1(&Character::myFunction, this, a);
}
};
Also I know that in order to have a pointer to a function in my class I can do it in this way:
#include <thread>
using namespace std;
class Character
{
private:
void (*FMoveForward)(int);// Pointer to a function.
public:
void setCommands(void(mf)(int delay)){//This function sets the pointer.
FMoveForward = mf;
}
void MoveForward(int delay){
FMoveForward(delay);// Here a call my function with my pointer to function.
}
};
My problem is that all the time when I try to use both of these things together the Visual Studio 13 compiler always complain about the sintaxe.
#include <iostream>
using namespace std;
class Character
{
private:
void (*FMoveForward)(int);
public:
void setCommands(void(mf)(int delay)){
FMoveForward = mf;
}
void MoveForward(int delay){
thread Mf1(&Character::FMoveForward , this, delay);// The VS 13 Complain because the sintaxe os this line.
}
};
Does anyone knows how to solve it? TY in advanced...
This
thread Mf1(&Character::FMoveForward ,0 this, delay);// The VS 13 Complain because the sintaxe os this line.
happens to have a syntax error: 0 this
The problem is that pointers to member functions are not pointers to free functions. std::thread
can use both, but you need to be consistent.
In your first example, you have a pointer to a member function. OK.
In your second example, you have a pointer to a free function. Also OK.
In your third example, FMoveForward
is a pointer to a free function. &Character::FMoveForward
is a pointer to a pointer. That's not going to work.
If you want to store &Character::myFunction
, you would need a void (Character::*FMoveForward)(int);
member. That's a pointer to a member function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.