简体   繁体   English

使用包含静态成员变量的类的向量

[英]Using vector of a class that contains static member variable

I had an Airplane class and this Airplane had a vector of Seat class named "m_seat". 我有一个飞机班,这个飞机有一个名为“ m_seat”的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. 在飞机的构造器中,我使用了座位数作为所需参数,以将m_seat矢量大小调整为用户要求的大小。 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, std :: vector <_Ty,_Aloc> :: push_back函数的重载不接受0个参数,

and if this was really the problem, I had no idea what should I have put in my push_back()? 如果这确实是问题,我不知道应该在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. 但是问题(m_seatNumber的数量增加相同)仍未解决。 Non-native English Speaker, Sorry. 非英语母语者,对不起。

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 eg 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. 如果可能,还复制一个Seat根本不会操纵该数字。 It'd be much better to allow the Airplane class to assign the seat numbers: 允许Airplane班分配座位号会更好:

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. 这样,您可以通过向飞机构造函数添加另一个参数来告诉它从哪个数字开始计数来获得旧的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM