请求非类类型的成员
Request for member which is non-class type
我有一个类MouseController。有一种方法 - 更新。
void MouseController::update(int x, int y) {
this->mX = x;
this->mY = y;
this->diffX = mX - 650;
this->diffY = mY - 350;
calculateAngle();
}
我正在使用过剩。我想制作glutPassiveMotionFunc并放置该更新功能。
glutPassiveMotionFunc(mouse.update);
我收到以下错误:
D:\projects\cpp\glut\main.cpp|129|error: 请求成员"更新" 在"鼠标"中,其非类类型为"鼠标控制器()"
问题
通过将mouse
定义为
MouseController mouse();
你定义了一个不带参数的函数,返回一个MouseController
,称为mouse
。然后,当您致电时
glutPassiveMotionFunc(mouse.update);
您尝试访问函数的成员update
mouse
。因此出现错误消息。
溶液
MouseController mouse;
(只有在MouseController::update(int,int)
是静态的时才有效,但事实并非如此。
真正的解决方案
MouseController mouse;
glutPassiveMotionFunc([&](int x, int y){mouse.update(x, y)});
这是glutPassiveMotionFunc
的签名
void glutPassiveMotionFunc(void (*func)(int x, int y));
如您所见,它采用一个带有两个整数作为参数的函数指针。在 C++ 中,无法获取指向非静态成员函数的指针,因为非静态成员函数具有隐式的第一个参数this
该参数对于类的每个实例都是唯一的。
您可以尝试在 MouseController 中存储一个指向更新函数的函数指针,然后将其传递给 glutPassiveMotionFunc
这将为您提供一种获取指向函数的指针的方法,但仍然与需要传递给glutPassiveMotionFunc
的函数指针的签名不匹配。查看其他响应者对 lambda 的解决方案来执行此操作。
相关文章:
- 根据模板类型选择类模板的成员类型?
- 函数模板签名中忽略的成员类型def 的访问说明符
- 在C++如何从数组中提取成员并返回成员类型的数组?
- C++从成员类型中扣除类型的功能模板?
- 无法初始化以 std::byte 作为成员类型的位字段
- 构造函数可以更改默认成员类型吗?
- 使用相同方法但不同成员类型构建类的最佳方法
- 允许通过指向方法的成员参数的指针来推断模板参数所指向的成员类型
- 使用 std::条件根据模板参数选择成员类型
- CRTP:如何推断要用作返回类型的成员类型?
- 对成员类型的成员方法使用 std::result_of<>
- C :可以从类及其受保护的成员类型继承可以继承吗?
- 如何在不指定不必要的模板参数的情况下使用模板类的成员类型
- 基类数据成员类型取决于派生类
- C 多态性:允许模棱两可的成员类型
- 使用使用成员类型别名的构造函数来推论类模板参数
- 获取与在模板参数中传递的函数成员类型相同的类
- 如果静态成员未初始化并且成员类型是类本身,该怎么办?
- boost::bind 无法绑定到纯虚拟基类中定义的非静态函数模板成员类型
- 将联合强制转换为其成员类型之一