无法从 class 中检索静态向量

Unable to retrieve a static vector from inside a class

我想从 class 城市获取矢量,但是我无法在显示的矢量 cities 中看到生成的任何城市...

但是我知道它们正在生成,因为我可以看到它们的名称以及正在增加的大小显示在构造函数中。

Contructor for: Hong Kong
1
Contructor for: Bangkok
2
Contructor for: Macau
3
Contructor for: Singapura
4
Contructor for: Londres
5
Contructor for: Paris
6
Contructor for: Dubai
7
Contructor for: Delhi
8
Contructor for: Istambul
9
Contructor for: Kuala
10
Contructor for: Lumpur
11
Contructor for: Nova Iorque
12
Contructor for: Antalya
13
Contructor for: Mumbai
14
Contructor for: Shenzen
15
Contructor for: Phuket
16

我做错了什么??下面是我的代码:

如您所见,我已经注释掉了析构函数,但是我仍然无法在向量中看到任何城市,因为调用函数 display_cities() 只会呈现 0,显示:

#include <iostream>
#include <ctime>
#include <vector>

using namespace std;

class City
{
private:
     string nome;
     static vector<string> cities;

public:
     static vector<string> getCidades() { return cities; }
     string getNome() { return nome; }
     City(string nome) : nome{nome}
     {
          this->cities.push_back(this->nome);
          cout << "Contructor for: " + this->nome << endl
               << this->cities.size() << endl;
     };
     // ~City(){};
} hongKong{"Hong Kong"}, bangkok{"Bangkok"}, macau{"Macau"}, singapura{"Singapura"}, londres{"Londres"}, paris{"Paris"}, dubai{"Dubai"}, delhi{"Delhi"}, istambul{"Istambul"}, kuala{"Kuala"}, lumpur{"Lumpur"}, novaIorque{"Nova Iorque"}, antalya{"Antalya"}, mumbai{"Mumbai"}, shenzen{"Shenzen"}, phuket{"Phuket"};

vector<string> City::cities;
void display_cities()
{
     vector<string> cities = City::getCidades();
     cout << cities.size() << endl;
     for (size_t i = 0; i < cities.size(); i++)
     {
          cout << cities[i] << endl;
     }
}

int main()
{

     display_cities();
     return 0;
}

您必须在 main 中创建一个城市对象。尝试将 main 中的所有这些构造函数作为城市对象传递。另外,我用 vs c++17 编译了你的原始代码,它抛出了一个异常。

int main() 
{
  City hongKong{ "Hong Kong" }, bangkok{ "Bangkok" }, macau{ "Macau" }, singapura{ "Singapura" }, londres{ "Londres" }, paris{ "Paris" }, dubai{ "Dubai" }, delhi{ "Delhi" }, istambul{ "Istambul" }, kuala{ "Kuala" }, lumpur{ "Lumpur" }, novaIorque{ "Nova Iorque" }, antalya{ "Antalya" }, mumbai{ "Mumbai" }, shenzen{ "Shenzen" }, phuket{ "Phuket" };
  display_cities();
  return 0;
}