我有以下类(Enemy类是父类,Boss和SuperDuperBoss是子类)。我的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
这是儿童班
#ifndef BOSS_H
#define BOSS_H
#include "Enemy.h"
class Boss : public Enemy
{
public:
void runAI();
};
class SuperDuperBoss : public Boss
{
public:
void runAI();
};
#endif
这是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;
}
我有链接错误。
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
答案 0 :(得分:1)
如果要覆盖父级的定义,则仅在派生类中声明runAI
。因此,假设您要使用Enemy
中的定义,您的派生类应如下所示:
class Boss : public Enemy
{
public:
//other bossy stuff
};
class SuperDuperBoss : public Boss
{
public:
//other superduperbossy stuff
};