0

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
CroCo
  • 5,531
  • 9
  • 56
  • 88
  • 3
    The declaration of `runAI()` in the child classes hides the implementation in the parent, and the linker error is because you haven't defined the actual function. Maybe you want to make those functions `virtual`? – Jonathan Potter Jul 13 '14 at 00:12
  • 2
    You're using a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – chris Jul 13 '14 at 00:21

1 Answers1

1

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
};
Pradhan
  • 16,391
  • 3
  • 44
  • 59