visual 从映射C++中的功能指针调用函数
visual Calling a function from a funtion pointer in a map C++
所以我不确定为什么这行不通,我尝试了一些谷歌搜索,我只是找不到问题是什么
void Player::Cmd(std::vector<std::string> &tokens)
{
std::string str = tokens[0];
std::map<std::string, void (Player::*)()>::iterator it = playerCommands.find(str);
Func fun;
if (it != playerCommands.end())
{
fun = it->second; //i tried it->second(); same issue
fun(); //error C2064: term does not evaluate to a
//function taking 0 arguments
}
else
{
std::cout << "What? n";
}
}
项目的 Git 中心https://github.com/lordkuragari/TextRPG
与
你的看法相反,你的地图没有函数指针。因此,您无法调用映射中的元素。
相反,映射包含指向成员函数的指针。非静态成员函数不是函数,不能调用;相反,必须在对象上调用它们。您可以在指针p
通过函数指针ptfm
的对象上调用成员函数:
(p->*ptmf)();
在您的情况下,大概您想使用 p = this
和 ptfm = fun
,所以它会是:
(this->*fun)();
或者,没有局部变量:
(this->*it->second)();
在 C++17 中,您还可以使用 std::invoke(it->second, this)
.
相关文章:
- 如何定义和设置指向模板类方法的功能指针
- 将状态传递给功能指针
- 如何使用结构内的功能指针调用私有函数
- 功能指针不起作用(C )
- 将C 方法作为功能指针传递
- 优化调用一系列功能指针
- C 功能指针和成员功能指针
- 当直接分配时,为什么此功能指针分配起作用,而不是与有条件的操作员一起使用
- 功能指针的静态初始化
- 功能指针参数参数转换为const
- 使用lambda在功能指针铸造时双免费
- 如何在两个类之间使用功能指针
- exc_bad_access on for loop(数组功能指针)
- 如何通过功能指针调用派生方法
- 虚拟功能指针指向派生类中的函数
- C :铸造成员功能指针
- 指向成员功能的功能指针
- 正确施放以指向返回功能的函数的功能指针
- 当用作模板参数时,功能指针是否需要指向具有外部链接的函数
- 在堆中创建功能指针的数组