我如何正确地从基类继承? [英] How do I correctly inherit from base class?

查看:74
本文介绍了我如何正确地从基类继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,名为飞机。我想将它设置为一个名为 fixedWing 子类。然后我希望 fixedWing 类有3个子类;第一个叫做 Glider ,第二个叫做 Jet ,第三个叫做 Propellers



我收到以下错误:



fixedwing.h(11):错误C2504:'飞机':基类未定义

fixedwing.h(35):警告C4584:'螺旋桨':基类'FixedWing'已经是'Jet'的基类

fixedwing.h(10):注意:看'FixedWing'的声明

fixedwing.h(26):注意:看看'Jet'的声明



我尝试过:



Aircraft.h

I have a class called Aircraft. I would like to set it to have a subclass called fixedWing. I would then like the fixedWing class to have 3 subclasses; The first called Glider, the second called Jet and the third called Propellers.

I am getting the following errors:

fixedwing.h(11): error C2504: 'Aircraft': base class undefined
fixedwing.h(35): warning C4584: 'Propeller': base-class 'FixedWing' is already a base-class of 'Jet'
fixedwing.h(10): note: see declaration of 'FixedWing'
fixedwing.h(26): note: see declaration of 'Jet'

What I have tried:

Aircraft.h

#pragma once

#include "stdafx.h"
#include "FixedWing.h"
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

class Aircraft;

class Aircraft{
	int regNo;
	string manufacturer;
	string serialNo;
	string productionDate;
	string owner;
	string annualInspection;
	int minSpeed;
	int maxSpeed;
	int maxClimbRate;
public:
	Aircraft(); // default construct
	Aircraft(int); 
};





FixedWing.h



FixedWing.h

#pragma once

#include "stdafx.h"
#include "Aircraft.h"
#include <string>

using namespace std;

// Fixed Wing class derived from Aircraft 
class FixedWing : public Aircraft
{
public:
	FixedWing();
	//FixedWing(int regNo);
};

// (sub#1) Gliders to fixed wing
class Glider : public FixedWing 
{
public:
	Glider();
private:
};

// (sub#2) Jets to fixed wing
class Jet : public FixedWing 
{
public:
private:
	int hoursInOperation;
};

// (sub#3) Propellers to fixed wing
class Propeller : Jet,FixedWing 
{
public:
private:
	int hoursInOperation;
};





我觉得解决方案正在盯着我,但我似乎无法找到它。任何人都可以帮助我吗?



非常感谢提前!



I feel that the solution is staring me in the face but I just can't seem to find it. Could anyone be able to assist me?

Many thanks in advance!

推荐答案

问题是你的Aircraft标题中的FixedWing标题是包括在内。那不行。你现在不需要这个标题。我的经验是,这个循环引用是一个糟糕的类架构的标志。



多重继承不是一个好主意,想想接口
The problem is that in your Aircraft header the FixedWing header is included. That wont work. You dont need this header for now in your class. My experience is, that this cyclic reference are a sign of poor class architecture.

Multiple inheritance isnt a good idea, think about interfaces.


删除飞机中 FixedWing.h 的包含。 ħ的。



这是你问题的根源,因为 FixedWing 定义是在飞机定义。



也不需要包含该文件,因为 FixedWing 未在 Aircraft.h 中引用。
Remove the inclusion of FixedWing.h in Aircraft.h.

That is the source of your problem because the FixedWing definition is so processed before the Aircraft definition.

There is also no need to include that file because FixedWing is not referenced in Aircraft.h.


这篇关于我如何正确地从基类继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆