【发布时间】:2022-11-16 12:01:19
【问题描述】:
我试图将许多函数指针推入一个向量以供以后使用。但是,我遇到了类型问题
/// in the h file
typedef std::function<int(unsigned char *)> func_t;
class A
{
void init();
// after some codes declaration
private:
B b;
std::vector<func_t> func_list;
}
class B
{
int somefunction(unsigned char *);
}
// elsewise in the cpp file of class A
A::init()
{
func_t f = std:bind(&B::somefunction, &b, std::placeholders::_1);
func_list.push_back(f);
}
错误似乎发生在 std::bind 点,错误读作
initializing: cannot convert from 'std::Binder<std::Unforced, void(__thiscall B::*)(unsigned char *), B*, const std::_Ph<1> &>' to std::function<int(unsigned char*)>
如果我将变量 f 从 func_t 更改为 auto ,问题就会消失。尽管随后我会遇到同样的问题来推入向量 func_list。所以我想我的问题是类型定义或 std::bind 定义
谢谢
【问题讨论】:
-
似乎对我有用:godbolt.org/z/j4j7d9vhe 你的编译器和 C++ 版本是什么?
-
@Ranoiaetep 我认为它会编译,但你应该得到一个运行时错误
-
@Ranoiaetep nvm,你是对的..不知道为什么我得到了 OP 一开始做的同样的错误..