c++ - 函数指针初始化不会编译
问题描述
我正在尝试初始化函数指针变量,以便将其作为参数传递给采用函数指针的构造函数。
我已经尝试了几乎可以在网上找到的该声明的每个版本,并且所有版本都给了我编译错误。
// the functions
void *OthelloBoard::GetOptions() const { return nullptr; }
void OthelloBoard::SetOptions(const void *data) {}
// the function pointers
void *(OthelloBoard::*GetOptionsPtr)() const = &OthelloBoard::GetOptions;
void (OthelloBoard::*SetOptionsPtr)(const void *) = &OthelloBoard::SetOptions;
// the constructor
BoardClass(void (*setOptions)(const void *), void *(*getOptions)()) {}
// the constructor call
BoardClass OthelloBoard::mClass(SetOptionsPtr, GetOptionsPtr);
我希望变量能够编译并正确传递,但我得到了这个错误:
error: cannot initialize a variable of type 'void (OthelloBoard::*)(const void *)' with an rvalue of type 'void (*)(const void *)'
void (OthelloBoard::*SetOptionsPtr)(const void *) = &OthelloBoard::SetOptions;
^ ~~~~~~~~~~~~~~~~~~~~~~~~~
解决方案
推荐阅读
- ffmpeg - FFMPEG - 如何在切断视频和音频的前几秒时对输入流进行转码
- multiprocessing - Pytorch:使用多处理 SharedMemory 和 CUDA 时 RAM 会爆炸
- php - 自定义验证 REDCap 挂钩
- sql - 有没有办法让查询返回某个条件的计数高于某个数字的用户?
- reactjs - 外部库的 VS Code 自动完成功能不起作用,JavaScript
- sql - SQL 从 ID 不重复的长表中选择案例
- javascript - 导入带有 js 文件的脚本标签到 head firefox 扩展
- python - 反复迭代较小的列表以将元素分配给较大的列表?
- python - 用 limit 方法计算 pi 会产生意想不到的结果
- authorize.net - Authorize.Net 无法更新到期日期