gpt4 book ai didi

c++ - C2276 '&' : illegal operation on bound member function expression

转载 作者:太空宇宙 更新时间:2023-11-04 12:37:43 28 4
gpt4 key购买 nike

我想在我的类 JQCollider 的构造函数中使用指向函数的指针作为参数。但是当我构造对象时出现此错误。

在出现错误“非标准语法;使用‘&’创建指向成员的指针”之前,我向每个指向成员参数的指针添加了‘&’。但是当我编译时,我现在遇到了这个错误。

class JQCollider
{
public:

JQCollider()
{

}
JQCollider(FloatRect getBounds(), Vector2f getPos(),void setPos(Vector2f pos))
{
getObjPos = getPos;
getObjBounds = getBounds;
setObjPos = setPos;
}

private:

Vector2f(*getObjPos)();
FloatRect(*getObjBounds)();
void(*setObjPos)(Vector2f);

}

class JQTextBox
{
public:
JQTextBox()
{
}

void Initialize()
{
collision = JQCollider(&textBoxSprite.getGlobalBounds, &textBoxSprite.getPosition, &textBoxSprite.setPosition);
}
private:
JQCollider collision;
Sprite textBoxSprite;
}


最佳答案

您不能创建指向特定实例的成员函数的函数指针。相反,使用 std::functionstd::bind :

#include <functional>

class JQCollider
{
public:
JQCollider()
{}

JQCollider(std::function<FloatRect>()> getBounds, std::function<Vector2f>()> getPos,
std::function<void(Vector2f)> setPos)
{
getObjPos = getPos;
getObjBounds = getBounds;
setObjPos = setPos;
}

private:
std::function<Vector2f>()> getObjPos;
std::function<FloatRect>()> getObjBounds;
std::function<void(Vector2f)> setObjPos;
}

class JQTextBox
{
public:
JQTextBox()
{}

void Initialize()
{
collision = JQCollider(std::bind(&Sprite::getGlobalBounds, textBoxSprite),
std::bind(&Sprite::getPosition, textBoxSprite),
std::bind(&Sprite::setPosition, textBoxSprite, std::placeholders::_1));
}

private:
JQCollider collision;
Sprite textBoxSprite;
}

关于c++ - C2276 '&' : illegal operation on bound member function expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55651688/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com