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

时间:2015-03-21 23:36:17

标签: c++

我遇到了一个奇怪的问题,我似乎无法找到答案,并认为我不妨问。

我有一个抽象类,它进行冲突检查,具有“更新”功能,以及“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;
};

在我让孩子调用更新函数进行冲突检查之前,我最近决定添加updateX和updateY函数。孩子现在调用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);
}

我已经多次选择了它,而且我没有发现任何愚蠢的事情,所以为什么更新不能被调用?

1 个答案:

答案 0 :(得分:4)

您正在将您的功能定义为全局功能:

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作为错误发出信号,因为全局范围内没有此类函数。

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

如果你这样写:

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

您会立即收到以下错误:

  

错误:&#39;这个&#39;仅允许在非静态成员函数中使用。

你会知道造成这个错误的原因。如果没有这个,很容易省略许多简单的错误。