gpt4 book ai didi

在抽象类中找不到 C++ 标识符

转载 作者:搜寻专家 更新时间:2023-10-31 01:01:28 28 4
gpt4 key购买 nike

我遇到了一个奇怪的问题,我似乎找不到答案,但我想我不妨问问。

我有一个执行碰撞检查的抽象类,它有一个“更新”函数,以及“updateX”和“updateY”函数。

class MapCollidable {
public:
MapCollidable(){}
~MapCollidable(){}

void update(const units::Coordinate2D origin, const Rectangle& boundingBox,
Rectangle& collider, units::Coordinate delta, Level* level,
Direction direction, bool moveAlongSlopes=false);

void updateX(const Rectangle& destRect, const Rectangle& boundingBox,
units::Coordinate delta_x, Level* level, bool moveAlongSlopes=false);
void updateY(const Rectangle& destRect, const Rectangle& boundingBox,
units::Coordinate delta_y, Level* level, bool moveAlongSlopes=false);


virtual void onCollision(const CollisionTile::CollisionInfo& collisionInfo) = 0;
virtual void onDelta(const CollisionTile::CollisionInfo& collisionInfo) = 0;
};

之前我只是让 child 调用更新函数来进行碰撞检查,但最近我决定添加 updateX 和 updateY 函数。 child 现在调用 updateX 和 updateY,后者调用 update。但是,当我尝试在 updateX 或 updateY 中调用更新时,出现“标识符‘update’未定义”错误。

void updateX(const Rectangle& destRect, const Rectangle& boundingBox,
units::Coordinate delta_x, Level* level, bool moveAlongSlopes=false) {

// Get some things ready to call update...
const units::Coordinate2D origin = units::Coordinate2D(destRect.getLeft(), destRect.getTop());
Direction direction = delta_x < 0 ? LEFT : RIGHT;
Rectangle collider ( boundingBox.getLeft() + destRect.getLeft() + delta_x,
boundingBox.getTop() + destRect.getTop(),
direction == LEFT ? ( boundingBox.getWidth() / 2 ) - delta_x : (boundingBox.getWidth() / 2) + delta_x,
boundingBox.getHeight() );


// Trying to call update here gives the error.
update(origin, boundingBox, collider, delta_x, level, direction, moveAlongSlopes);
}

我已经挑了好几次了,我没有发现我做过的任何愚蠢的事情,那么为什么不能调用更新呢?

最佳答案

您将函数定义为全局函数:

void updateX(const Rectangle& destRect, const Rectangle& boundingBox,
units::Coordinate delta_x, Level* level, bool moveAlongSlopes=false) {
//...
}

将其更改为:

void MapCollidable::updateX(const Rectangle& destRect, const Rectangle& boundingBox,
units::Coordinate delta_x, Level* level, bool moveAlongSlopes=false) {
//...
}

update 的调用被标记为错误,因为在全局范围内没有这样的函数。

顺便说一句,这就是为什么我总是使用this引用任何成员函数/变量。

如果你这样写:

void updateX(const Rectangle& destRect, const Rectangle& boundingBox,
units::Coordinate delta_x, Level* level, bool moveAlongSlopes=false) {
//...
this->update(...);
}

您会立即收到以下错误:

Error: 'this' only allowed in non-static member functions.

你会知道是什么导致了这个错误。如果没有 this,许多简单的错误很容易被忽略。

关于在抽象类中找不到 C++ 标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189286/

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