我收到此错误:C ++-此声明在该行中没有存储类或类型说明符:

towerControl.push_back() = new Boeing;

有人可以指出出什么问题了吗?谢谢。
#include <iostream>
#include <vector>
using namespace std;

class Aircraft {
public:
    virtual void disp() { cout << "I'm an aircraft\n"; }
    virtual ~Aircraft() { cout << "Aircraft::Destructor\n"; }
};

class Boeing: public Aircraft {
public:
    void disp() { cout << "I'm a Boeing\n"; }
    ~Boeing() { cout << "Boeing::Destructor\n"; }
};

class Airbus: public Aircraft {
public:
    void disp() { cout << "I'm an Airbus\n"; }
    ~Airbus() { cout << "Airbus::Destructor\n"; }
};

class Tower {
public:
    vector <Aircraft*> towerControl;
    towerControl.push_back() = new Boeing;
    towerControl.push_back() = new Airbus;
    ~Tower() { cout << "Tower::Destructor\n"; }
};

int main() {
    Tower T;
}

最佳答案

当然。语法错误。它应该是:

towerControl.push_back(new Boeing);


而且您还需要在Tower的成员(可能是构造函数)中调用它:

class Tower {
public:
    vector <Aircraft*> towerControl;
    Tower()
    {
       towerControl.push_back(new Boeing);
       towerControl.push_back(new Airbus);
    }
    ~Tower() { cout << "Tower::Destructor\n"; }
};


在我看来,您需要开始学习C ++的基础知识。

关于c++ - C++ —此声明没有存储类或类型说明符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9676192/

10-11 18:00