【发布时间】:2021-02-17 00:49:31
【问题描述】:
class Entity {
public:
virtual void applyCollisionBehaviorTo(Entity &entity) { }
virtual void onCollision(Entity &entity) { }
};
class Ball : public Entity {
public:
void applyCollisionBehaviorTo(Entity entity) override {
}
void onCollision(Entity entity) override {
entity.applyCollisionBehaviorTo(this); // error: no matching function for call to 'Entity::applyCollisionBehaviorTo(Ball*)'
}
};
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
我来自 C# 背景,所以我正在研究 C++ 继承和多态性。
【问题讨论】:
-
因为那时还没有声明。你以后才声明它。
-
另外,基类不应该知道派生类!
-
你没有。它首先使用继承打败了整个观点。您是否正在尝试实现某种双重调度?
-
为了通过值传递
Ball,您需要在使用之前对其进行完整定义。但是您可以只使用前向声明来传递指针或引用。 -
我已更新问题以反映更多问题所在。