몇 가지 코드 :전달 함수 포인터 X 인수 여기
// Unit.h
typedef void (IInteractable::*fPtr)(Player&);
typedef std::vector<std::pair<std::string, fPtr>> Actions;
// Unit.cpp
Actions Unit::getActions()
{
Actions a;
a.push_back(make_pair("Attack", &Unit::attack));
return a;
}
void attack(Player& p)
{
cout << "Attack!" << endl;
}
그리고 내가 뭘 얻을 것은 이상한 원인 단위 IInteractable에서 상속 보인다 error: no matching function for call to ‘std::vector<std::pair<std::basic_string<char>, void (IInteractable::*)(Player&)> >::push_back(std::pair<const char*, void (Unit::*)(Player&)>)’
이다. 이 문제를 "단순"하게 만드는 데는 시간이 걸렸지 만 다음에 무엇을해야할지 전혀 모릅니다. 누구든지 도와 줄 수 있습니까?
작동하지 않습니다. 위의 예에서 문제가없는 것처럼 보입니다.하지만 누군가가 따라 오면 벡터의 요소를 꺼내서 함수 포인터를 호출합니다 (벡터의 def에 따라 * IInteractable :: *) '단위는 아니지만'IInteractable '에 무엇이 있습니까? 그때 당신은'Unit'이 아닌 무언가에 대해'Unit' 멤버 함수를 호출합니다, 그리고 그것은 * bad *입니다. 이것이 컴파일러가 원하는 것을 할 수없는 이유입니다. – us2012
관련 : http://stackoverflow.com/questions/4387798/function-pointers-to-member-functions-of-a-parent-class –