[英]non-standard syntax; use '&' to create a pointer to member error when assigning member function to vector
Im trying to add a member function of Player Class to a vector of functions. 我试图将Player类的成员函数添加到函数向量中。
This is the vector of functions that require a Keyboard::Key as parameter. 这是需要使用Keyboard :: Key作为参数的函数的向量。
vector<function<void(Keyboard::Key)>> KeyEvent;
Then i have the Players implementation 然后我有播放器实现
Player::Player() {
KeyEvent.push_back(OnKey); //error: non-standard syntax; use '&' to create a pointer to member
}
void Player::OnKey(Keyboard::Key key) {
//do something with key...
}
I have other instances where something code similair works fine, but not with member functions. 我还有其他一些类似的代码可以正常工作的实例,但不能使用成员函数。
vector<function<void()>> UpdateEvent;
UpdateEvent.push_back(HandleInputUpdate);
void HandleInputUpdate() {
for (size_t i = 0; i < pressedKeys.size(); i++) {
for (size_t e = 0; e < KeyEvent.size(); e++) {
KeyEvent[e](pressedKeys[i]);
}
}
}
Is it possible to add an a member function to this vector and if not, how could I create something similair? 是否可以向此向量添加一个成员函数,否则,如何创建类似符号? I am very new to C++ and I created the vector so I could replicate C# Actions. 我对C ++非常陌生,因此创建了矢量,以便可以复制C#Action。 This is how far i have come until I ran into problems. 在遇到问题之前,这就是我走的路。 This is an early concept as I still need to add a way to remove functions from the vector. 这是一个早期的概念,因为我仍然需要添加一种从向量中删除函数的方法。
OnKey
is a non-static member function of Player
and thus requires an instance of Player
to be called on. OnKey
是Player
的非静态成员函数,因此需要调用Player
的实例。 Even if you got the pointer-to-member-function syntax right, you'd still have that exact problem, because there is no automatic binding; 即使您正确使用了指向成员函数的语法,也仍然会遇到确切的问题,因为没有自动绑定。 in other words, the instance pointed to by this
is not automatically used to produce a callable object usable as a std::function<void(Keyboard::Key)>
. 换句话说,该实例指向this
不会自动用于生产可用作一个可调用对象std::function<void(Keyboard::Key)>
。
A very easy way to achieve what you want to do here is to use a lambda: 一个简单的方法来实现您要在此处执行的操作是使用lambda:
KeyEvent.push_back([this](auto key) { OnKey(key); });
The OnKey(key);
OnKey(key);
call in the function is equivalent to this->OnKey(key);
函数中的调用等效于this->OnKey(key);
. 。
A more complicated (in my opinion) way would be to use std::bind
: 一种更复杂的方法(在我看来)是使用std::bind
:
using namespace std::placeholders;
KeyEvent.push_back(std::bind(&Player::OnKey, this, _1));
Generally speaking, with lambdas and std::function
in modern C++, use cases for pointers to member functions have become extremely rare. 一般来说,对于现代C ++中的lambdas和std::function
,指向成员函数的指针的用例已变得极为罕见。
UsePlayer :: OnKey(仅具有OnKey甚至&OnKkey)不足以用于指针到成员的情况。
Is it possible to add an a member function to this vector 是否可以向此向量添加成员函数
No. Non-static member function pointers are quite a bit different than non-member function pointers and static member function pointers. 否。非静态成员函数指针与非成员函数指针和静态成员函数指针有很大不同。
and if not, how could I create something similair? 如果没有,我怎么能创造出类似的东西?
Use a lambda expression. 使用lambda表达式。 In the lambda expression, make sure to capture the this
pointer and use it in the function call. 在lambda表达式中,确保捕获this
指针并在函数调用中使用它。
KeyEvent.push_back([this](Keyboard::Key key) {this->OnKey(key);});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.