当我尝试使用指向不同类的成员函数的指针时,出现了此错误。
错误:sources / common / OurMenu.cpp: 在成员函数“ void OurMenu :: menuNavigation()”中:sources / common / OurMenu.cpp:336:36:错误:必须使用“。*”或“-> *”在 '(((OurMenu *)this)-> OurMenu :: _ buttons.std :: vector :: at>((((std :: vector :: size_type)((OurMenu *)this)- > OurMenu :: _ actualLine))-> Button :: doIt(...)',例如'(...-> *((OurMenu *)this)-> OurMenu :: _ buttons.std :: vector :: at >((((std :: vector :: size_type)((OurMenu *)this)-> OurMenu :: _ actualLine))-> Button :: doIt)(...)' _buttons.at(_actualLine)-> doIt( );
那是我的代码:
OurMenu.hh
class OurMenu
{
private:
GraphicLib *_window;
OurGame *_game;
gdl::Geometry _background;
std::vector<gdl::Texture*> _textures;
std::vector<Button*> _buttons;
int _actualLine;
int _time;
public:
[...]
void menuNavigation();
void gameSetting();
};
Button.hh
class Button
{
private:
Bloc *_select;
Bloc *_notSelect;
public:
Button(const float &x, const float &y, gdl::Geometry *back,
gdl::Texture *tx, gdl::Texture *txS, glm::vec3 const &scale, void (OurMenu::*ptr)());
~Button();
void (OurMenu::*doIt)();
void isSelect();
void isDeselect();
Bloc *getSelect() const;
Bloc *getNotSelect() const;
};
按钮声明
Button *solo = new Button(2, 7, &_background, _textures.at(1), _textures.at(7),
glm::vec3(0.0025, 0.0022, 0), &OurMenu::gameSetting);
函数调用
solo->doIt();
Button.cpp
Button::Button(const float &x, const float &y, gdl::Geometry *back,
gdl::Texture *tx, gdl::Texture *txS, glm::vec3 const &scale, void (OurMenu::*ptr)())
{
_select = new Bloc(0, 0, back, txS);
_select->rotate(glm::vec3(1, 1, 0), 180);
_select->setScale(scale);
_select->setX(x);
_select->setY(y);
_select->setZ(1);
_notSelect = new Bloc(0, 0, back, tx);
_notSelect->rotate(glm::vec3(1, 1, 0), 180);
_notSelect->setScale(scale);
_notSelect->setX(x);
_notSelect->setY(y);
_notSelect->setZ(-1);
doIt = ptr;
}
就您而言,您似乎在尝试
(solo->doIt)(); // need the parentheses here
wheresolo
是type Button*
,但是doIt
是指向type的成员函数的指针void (OurMenu::*)()
。这将不起作用,因为在您的情况下,必须在相同类型的实例上调用成员函数的指针OurMenu
。如果solo
可以强制转换为OurMenu
(可能不是),则可以执行
(static_cast<OurMenu*>(solo)->doIt)();
否则,您应该更改设计。看来您想从Button
实例中调用菜单功能。如果是这样的话,你需要创建的实例OurMenu
你Button
,并调用该实例上的指针,像
OurMenu menu; // this is created somewhere in Button
(menu.*doIt)();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句