简体   繁体   中英

Arduino C++ Inheritance and function declaration problem

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:

}

I come from a C# background so I'm getting my head around C++ inheritance and polymorphism.

Your class Entity should be like this:

class Entity
{
public:
    virtual void applyCollisionBehaviorTo(Entity &entity) = 0;    
    virtual void onCollision(Entity &collidingEntity) = 0;
};

You can't refere to object of Ball class inside Entity , in fact entities don't even know about the existence of balls.
OTOH balls "know" that they are entities

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM