导入派生类只能导入一个

Importing derived classes can only import one

本文关键字:导入 一个 派生      更新时间:2023-10-16

如何导入多个派生类标头(不收到错误(?

我有一艘基础船。我有2个派生的课程,货物和游轮。在我的主要课程中,我尝试导入货物和巡航的标题文件。如果我两者都导入,我会发现一个错误;如果我导入一个,没有错误。

我对多个进口的尝试:

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

错误列表:

1>c:userscorysourcereposproject33project33ship.h(7): error C2011: 'Ship': 'class' type redefinition
1>c:userscorysourcereposproject33project33ship.h(7): note: see declaration of 'Ship'
1>c:userscorysourcereposproject33project33cargoship.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;
}

不使用基类标题文件上的guards:

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