1

I am relatively new to C++ and I am in the process of creating a hypothetical 'Flight Management' application as a learning experience and to test out various new skills. Simply put, I have a FlightPlan class which among other things contains a private member pointer to an object of type Aircraft (well, for now).

Objects derived from Aircraft are pre-existing at the time of FlightPlan creation, and I want to be able to assign an Aircraft derived object pointer to a FlightPlan (the a/c that will carry out the flight in the plan) after a FlightPlan is constructed and not just during construction. (using a method such as

flight-plan-obj.AssignAircaft(cargo-aircraft-obj-pointer);

My issue is that Aircraft is an abstract class that CargoAircraft and PassengerAircraft derive from, therefore it is not known what 'type' of aircaft will be assigned to the flight plan during it's construction. My code is below. (Much has been left out for brevity)

class FlightPlan {
    private:
    string departure;
    string destination;

    Aircraft* aircraft; // The a/c that will later be assigned to the flight
                       // Obviously this is currently wrong

    public:
    FlightPlan(string dep, string dest);
    void AssignAircraft(Aircraft* ac);

    // Other methods
}

The Aircraft abstract class:

class Aircraft {
    protected:
    Aircaft(string tailId, string acType); // ctor for the sub-classes

    string tailId;
    string acType;

    public:
    virtual string Overview() const =0; // pure virtual method 
}

And an example of an object derived from Aircraft:

class CargoAircraft : public Aircraft {
    private:
    int cargoCapacity;

    public:
    CargoAircraft(string tailId, string acType, int cargoCapcity);
    string Overview() const override;

}
    // ctor defined in the .cpp file as:
    CargoAircraft::CargoAircraft(string tailId, string acType, int cargoCapacity)
    : Aircraft(tailId,acType), cargoCapacity(cargoCapacity) {};

Is what I'm trying to do possible? My understanding was that pointer polymorphism would allow me to do:

Aircraft* aircraft; or Aircaft* aircraft = nullptr;

in FlightPlan and that would later accept an object pointer of a class derived from Aircraft? On compile I receive: error: ‘Aircraft’ does not name a type

Is there something fundamentally wrong with my approach here? Do I need to give up on Aircraft being abstract and just treat it as more of a base class and give it a public default constructor and make it's methods just virtual instead of pure-virtual?

Any and all advice will be graciously received, and I will gratefully accept any other hints or tips regarding my use of C++. Thank you.

ajgio23
  • 55
  • 5

1 Answers1

2

You can do two things,

in FlightPlan and that would later accept an object pointer of a class derived from Aircraft? On compile I receive: error: ‘Aircraft’ does not name a type

Header must be included with the class Aircraft so it is declared before Flightplan. Or you can use forward declaration like this

class Aircraft;

Telling the compiler of the type Aircraft that it exists and the pointer can be declared you use in the class Flightplan.

Your problem is only about the existence of the type Aircraft. Use forward declaration or guarantee that the class is declared before you declare Flightplan.

P.S. don't forget about virtual destructor in Aircaft class

Community
  • 1
  • 1
Gerhard Stein
  • 1,543
  • 13
  • 25
  • Thanks for remembering that. – Gerhard Stein Feb 13 '16 at 23:35
  • Your advice was well received, thank you. I already had the Aircraft.h in the FlightPlan.h but that wasn't doing it for me. The forward declaration did, and I have since used that to solve some other issues too. Thanks again. – ajgio23 Feb 14 '16 at 11:01