在C ++中继承时出错

时间:2012-04-17 03:33:05

标签: c++ inheritance

class RandomFleet : public PlaceFleet
{

这是我的RandomFleet,它需要从Placefleet继承。 (这是在RandomFleet.h中)

我的错误:无效使用未定义类型'class Battleships :: PlaceFleet'

我收录了“PlaceFleet.h” 我已经尝试过类Battleships :: RandomFleet:public BattleShips :: PlaceFleet

我无法弄清楚为什么它不能正常继承。

编辑:也不是PlaceFleet和RandomFleet都是'Battleships'名称的一部分 - 我不是“使用名称空间Battleships”所以我怀疑这与它有关吗?

使用代码编辑:

#ifndef PLACEFLEET_H_
#define PLACEFLEET_H_
#include"Place.h"
#include"Board.h"
class Battleships::PlaceFleet
{ //Functions and variables };
#endif
上面的

//是PlaceFleet.h,下面是RandomFleet.h

#ifndef RANDOMFLEET_H_
#define RANDOMFLEET_H_
#include"PlaceFleet.h"
class RandomFleet : public PlaceFleet
{//This is where compiler shows error
//Functions and variables
};
#endif

2 个答案:

答案 0 :(得分:2)

这是您应该遵循的模板:

// PlaceFleet.h
#ifndef PLACEFLEET_H_
#define PLACEFLEET_H_

class PlaceFleet {

};

#endif

// RandomFleet.h
#ifndef RANDOMFLEET_H_
#define RANDOMFLEET_H_

#include "PlaceFleet.h"

class RandomFleet : public PlaceFleet {

};

#endif

答案 1 :(得分:0)

因为我没有使用

namespace Battleships
 {
   //classes here
 }

(我试图上课Battleships :: PlaceFleet)