导入派生类只能导入一个

时间:2018-02-07 02:13:53

标签: c++

如何导入多个派生类标题(没有收到错误)?

我有一个基类船。我有2个派生类,货物和游轮。在我的主要课程中,我尝试导入cargoship和cruiseship的头文件。如果我同时导入两者,我会收到错误;如果我导入一个,没有错误。

我尝试多次导入:

include "CruiseShip.h"
include "Cargoship.h" 

错误列表:

1>c:\users\cory\source\repos\project33\project33\ship.h(7): error C2011: 'Ship': 'class' type redefinition
1>c:\users\cory\source\repos\project33\project33\ship.h(7): note: see declaration of 'Ship'
1>c:\users\cory\source\repos\project33\project33\cargoship.h(7): error C2504: 'Ship': base class undefined

我的船基类:

#include "Ship.h"

Ship::Ship() {
}
 Ship::~Ship() {
 }

Ship::Ship(string name, string year) : shipName(name), shipYear(year) {

 }

string Ship::getShipName() {
 return shipName;
}
string Ship::getShipYear() {
 return shipYear;
}

 void Ship::setShipName(string name) {
  shipName = name;
}

void Ship::setShipYear(string year) {
 shipYear = year;
 }

 string Ship::print() const {
 return "Ship Name: " + shipName + " \nYear Built: " + shipYear;
}

1 个答案:

答案 0 :(得分:-1)

未在基类头文件中使用包含保护:

https://en.wikipedia.org/wiki/Include_guard