CroCo
CroCo

Reputation: 5741

inheritance of child from another child classes

I have the following classes ( Enemy class is the parent class and Boss and SuperDuperBoss are children classes). My problem with SuperDuperBoss

#ifndef _ENEMY_H
#define _ENEMY_H

#include <iostream>

    class Enemy
    {
    public:
        void SelectAnimation();
        inline void runAI() 
        {
            std::cout << "RunAI() is running ...\n";
        }

    private:
        int m_iHiPoints;
    };

    #endif

and this is the children classes

#ifndef BOSS_H
#define BOSS_H

#include "Enemy.h"



class Boss : public Enemy
{
 public:
    void runAI();
};

class SuperDuperBoss : public Boss
{
public:
    void runAI();
};

#endif

This is the main.cpp

#include "Enemy.h"
#include "Boss.h"

int main()
{
    Enemy enemy1;
    Boss boss1;
    SuperDuperBoss supBoss;

    enemy1.runAI();
    boss1.runAI();
    supBoss.runAI();  // <--- Error: linking

    return 0;
}

I have a linking error.

Undefined symbols for architecture x86_64:
  "SuperDuperBoss::runAI()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Error 1

Upvotes: 0

Views: 52

Answers (1)

Pradhan
Pradhan

Reputation: 16737

Only declare runAI in the derived classes if you want to override the parent's definition. So, assuming you want to use the definition from Enemy, your derived classes should look like this:

class Boss : public Enemy
{
 public:
 //other bossy stuff
};

class SuperDuperBoss : public Boss
{
public:
//other superduperbossy stuff
};

Upvotes: 1

Related Questions