C++ 将成员函数作为参数传递给另一个成员函数

C++ passing a member function as an argument to another member function

我想将一个成员方法作为参数传递给另一个成员方法。我对此进行了广泛的研究,但仍然看不出它是正确的。我的头文件如下

#include <string>
#include <map>
#include <functional>

#include "Entity.h"
#include "World.h"
#include "Item.h"

class Tile;

class Player : public Entity
{
private:
    using FunctionPointer = void (Player::*)(Tile*);
    bool victory;
    Point location;
    std::map<char, FunctionPointer> actions;

public:
    Player(std::string name, int gold, int maxHitPoints, int defensePoints, 
           Point startingLocation, int maxDamage = 0, int maxItems = -1,
           std::vector<Item*> inventory = std::vector<Item*>());

    std::string getClassName() const override;

    void printInventory(Tile*) override; 
    std::string toString() override;

    Point getLocation() const;
    Item* findMostPowerfulWeapon();
    void heal(Tile*);
    void moveNorth(Tile*);
    void moveSouth(Tile*);
    void moveEast(Tile*);
    void moveWest(Tile*);
    void attack(Tile* tile);
    void pickup(Tile* tile);
    void trade(Tile* tile);

    void getAvailableActions(Tile* tile);
    void chooseAction();

private:
    void move(int dx, int dy);
    void actionAdder(char hotkey, FunctionPointer, std::string name);
}; 

给我带来问题的 cpp 文件的一部分如下:

void Player::getAvailableActions(Tile * tile)
{
    actions.clear();
    std::cout << "Choose an action:" << std::endl;
    if (getInventory().size() > 0)
        actionAdder('i', (this->*(&Player::printInventory))(tile), "Print inventory");
    if (tile->getClassName() == "Trader")
        actionAdder('t', (this->*(&Player::trade))(tile) , "Trade");
    if (tile->getClassName() == "Monster")
        actionAdder('a', (this->*(&Player::attack))(tile), "Attack");
}

void Player::actionAdder(char hotkey, FunctionPointer action, std::string name)
{}

Visual studio 标记所有三个 this 前面的括号,(this->*(&Player::attack))(tile),并给出工具提示 "argument of type "void" is incompatible参数类型为 "Player::FunctionPointer"”。如果我尝试编译,我得到的编译器错误是 'void Player::actionAdder(char,Player::FunctionPointer,std::string)': cannot convert argument 2 from 'void' to 'Player::FunctionPointer'.

如果有人知道我做错了什么,我将不胜感激。如果您需要查看更多代码或更多详细信息,请告诉我。代码不是超级机密。

谢谢

仔细阅读错误信息:

argument of type "void" is incompatible with parameter of type

cannot convert argument 2 from 'void' to 'Player::FunctionPointer'.

那是因为这里的参数 2:

actionAdder('i', (this->*(&Player::printInventory))(tile), "Print inventory");

实际上是 调用 printInventory 并试图将调用的结果传递给 actionAdder()。但那是一个 void 函数,你不能将 void 类型的东西传递给其他东西 - 因此错误正是抱怨那个。

您不想调用 printInventory,您只想传递一个指向它的指针。那只是:

actionAdder('i', &Player::printInventory, "Print inventory");