0

I had an Airplane class and this Airplane had a vector of Seat class named "m_seat". In the Constructor of my Airplane, I used the number of seats as the needed parameter to resize the m_seat vector size to the requested size of the user. This was my code:

class Seat;

class Airplane {
    vector<Seat> m_seat;
public:
    Airplane(int);
};

class Seat{
    static int m_seatNumber;
public:
    Seat() { m_seatNumber += 10; }
};

int Seat::m_seatNumber = 100;

Airplane::Airplane(int numberOfSeats) {
    for (int i = 0; i<numberOfSeats; ++i) {
        m_seat.push_back();
    }
}

int main()
{
    Airplane(80);
    return 0;
}

But it gave this error.

std::vector<_Ty,_Aloc>::push_back no overload of function takes 0 arguments,

and if this was really the problem, I had no idea what should I have put in my push_back()? So I tried {}

m_seat.push_back({});

and It worked!

Now, I have another problem which is my main problem(SO rule: Ask only one question at a time!) that all seat numbers appear to be increased to the same number! I also used the "resize" member function of the vector, instead of that loop:

m_seat.resize(numberOfSeats);

But the problem (same increase in the number of the m_seatNumber) remains unsolved. Non-native English Speaker, Sorry.

M-J
  • 1,028
  • 11
  • 15
  • You push a `Seat` object plain and simple? That the `Seat` class have a static member variable should not really matter, should it? It seems your problem is more of a design-problem (or rather, the *lack* of a design) than a code-problem. – Some programmer dude Jun 06 '17 at 12:54
  • "that all seat numbers appear to be increased to the same number! " - sorry, could you rephrase it? What are you expecting, and what are you seeing? Did you want each seat to have a distinct, increasing number? – Bartek Banachewicz Jun 06 '17 at 12:55
  • 1
    First off, why is `m_seatNumber` static? Shouldn't each seat have its own number? – NathanOliver Jun 06 '17 at 12:55
  • 3
    "all seat numbers appear to be increased to the same number". Well no. You simply are incorrect in thinking that you have more than one seat number in the first place. There's only one `int`, associated with the `Seat` class, and no data at all associated with each `Seat` instance. That's what the `static` keyword means. – Ben Voigt Jun 06 '17 at 12:55
  • 1
    And as for what you're putting into `.push_back`, it needs to be a `Seat` value - that's what the vector stores, after all. `{}` in this case is equivalent to `Seat()`, so a default-constructed value. And don't call your static member variables `m_`. – Bartek Banachewicz Jun 06 '17 at 12:55
  • You might be interested in following a [good C++ book](https://stackoverflow.com/q/388242/1782465) to get a firmer understanding of concepts such as static class members. – Angew is no longer proud of SO Jun 06 '17 at 12:57
  • @Someprogrammerdude I want an airplaine that has seats and each seat has several features, including a number that is generated automatically and represents that seat in the airplane. I think my design is telling the same thing... – M-J Jun 06 '17 at 13:01

1 Answers1

1

Disclaimer: This is a "best guess" answer.

If you wanted each seat to have a different, automatically increasing number, you need two values; one non-static, describing each seat, and one static, describing last-used number:

class Seat{
    static int lastSeatNumber;
    int seatNumber;

public:
    Seat() { seatNumber = lastSeatNumber; lastSeatNumber += 10; }
};
int Seat::lastSeatNumber = 100;

That way each seat will receive its distinct number. This design is bad, however, as it doesn't allow e.g. seat number sharing between two airplanes! It also doesn't allow you to "free up" the numbers of seats you're no longer using, and the number can only keep growing. Also copying a Seat, while possible, won't manipulate that number at all. It'd be much better to allow the Airplane class to assign the seat numbers:

class Seat{
    int seatNumber;

public:
    Seat(int seatNumber) : seatNumber(seatNumber) { }
};

Airplane::Airplane(int numberOfSeats) {
    int seatNumber = 100;
    const int numberIncrement = 10;

    for (int i = 0; i < numberOfSeats; ++i) {
        m_seat.push_back(Seat(seatNumber));
        seatNumber += numberIncrement;
    }
}

This way you can get the old behavior by adding another parameter to the airplane constructor telling it which number to start counting from.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135