remax
remax

Reputation: 29

Classes and inheritance

I am new to OOP and I have encountered a problem while writing my first code. I do not understand why I can not use one class as a part of another. And no I do not want that class to inherit another. One of the requirements is that I prevent object copying.

    #pragma once
#include<iostream>
#include<string>
using namespace std;
class Pilot 
{

public:
    Pilot(/*string x*/) 
    {
        setName();
        flight_hours = 0;
        set_status(0);
    }
    void setName(/*string x*/) 
    {
        cout<<"Unesi ime pilota: ";
        getline(cin,name);
    }
    string getName() 
    {
        return name;
    }
    void increase_flight_hours(int n) 
    {
        flight_hours += n;
    }
    int get_flight_hours() 
    {
        return flight_hours;
    }
    void set_status(bool b) 
    {
        status;
    }
    bool get_status() 
    {
        return status;
    }
    void display_pilot() 
    {
        cout << name;
        cout << "(", flight_hours, ")";
        if (status)
            cout << "-L" << endl;
        else
            cout << "-N" << endl;
    }

    Pilot (const Pilot&) = delete;
    void operator=(const Pilot&) = delete;

private:
    string name;
    int flight_hours;
    bool status;




};


#pragma once
#include"Pilot.h"

class Avion 

{
public:

    Avion ()
    {
        setName();
        set_capacity();
    }

    void setName(/*string x*/)
    {
        cout << "Unesi ime aviona: ";
        getline(cin, name);
    }
    string getName()
    {
        return name;
    }
    void set_capacity()
    {
        cout << "Unesite kapacitet aviona: ";
        cin >> capacity;
    }
    int get_capacity() 
    {
        return capacity;
    }

    Pilot get_captain() 
    {
        return captain;
    }

private:
    string name;
    Pilot captain;
    Pilot copilot;
    int capacity;
};

I get this error : function "Pilot::Pilot(const Pilot &)" (declared at line 50 of "C:\Users\mjova\source\repos\Project1\Project1\Pilot.h") cannot be referenced -- it is a deleted function Project1 C:\Users\mjova\source\repos\Project1\Project1\Planes.h 36

Upvotes: 0

Views: 77

Answers (1)

molbdnilo
molbdnilo

Reputation: 66371

One problem is here:

Pilot get_captain() 
{
    return captain;
}

which returns a copy of the captain, and you have expressly disallowed copying.

Return a const reference instead:

const Pilot& get_captain() 
{
    return captain;
}

(and don't try to copy what it returns).

There could also be some other copying-related code in "Planes.h"; it's not clear whether Avion is defined in that file or not.

Side note: since you can't copy Pilots, the captain and copilot members of Avion are problematic (you can't implement set_captain, for instance).
I suspect that you're going to want to change them to be pointers in the future, and let pilots exist without planes and vice versa.

Upvotes: 2

Related Questions