0

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

rustyx
  • 80,671
  • 25
  • 200
  • 267
remax
  • 29
  • 5
  • 8
    `Pilot get_captain() ` is a copy. You want `Pilot& get_captain() ` – ChrisMM Oct 30 '19 at 12:10
  • 4
    `using namespace` in a header is rude to everyone else who `include`s it. – Mark Storer Oct 30 '19 at 12:11
  • 2
    read [this](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). TL;DR: never use `using namespace std;` in a header, in sources i cannot do that much harm, but still the downsides are more than the advantages – 463035818_is_not_an_ai Oct 30 '19 at 12:16
  • @ChrisMM rather `const Pilot& get_captain()`, as returning a non-const reference of a private member defeats the purpose of making it private – 463035818_is_not_an_ai Oct 30 '19 at 12:18
  • Construction should not interact with the user, and neither should setters (also, avoid setters when possible). Read the proper values outside and pass them as arguments. – molbdnilo Oct 30 '19 at 12:18

1 Answers1

2

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.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82