gpt4 book ai didi

C++ vector 问题 - 'LNK2001: unresolved external symbol private: static...'

转载 作者:IT老高 更新时间:2023-10-28 21:59:11 25 4
gpt4 key购买 nike

在有人指责我不查看预先存在的问题之前,我已经查看并意识到它与声明有关,但我仍然无法让它工作(可能与我使用 vector 有关)。

Manager.h:

#include "Flight.h"
#ifndef manager_h
#define manager_h

class Manager {
static vector<Airport> airports;
static vector<Flight> flights;
public:
static void loadAirports();
static void loadFlights();
static Airport getAirport(string code);
static vector<string> split(const string &s, vector<string> &elems);
};

#endif

Manager.cpp:

#include "Manager.h"

void Manager::loadAirports ()
{
ifstream airportfile("airports.txt");
string line;
while (getline(airportfile, line))
{
vector<string> values;
split(line, values);
Airport airport (values[0], values[1], atoi(values[2].c_str()));
airports.push_back(airport);
}
}

void Manager::loadFlights ()
{
ifstream flightfile("flights.txt");
string line;
while (getline(flightfile, line))
{
vector<string> values;
split(line, values);
Flight flight (getAirport(values[0]), getAirport(values[1]), atoi(values[2].c_str()), atoi(values[3].c_str()));
flights.push_back(flight);
}
cout << flights.size() << endl;
}

Airport Manager::getAirport (string code)
{
for (int i = 1; i < (int)airports.size(); i++)
{
if (airports[i].code == code)
return airports[i];
}
throw exception();
}

vector<string> Manager::split(const string &s, vector<string> &elems) {
stringstream ss(s);
string item;
while(getline(ss, item, ',')) {
elems.push_back(item);
}
return elems;
}

它正在抛出这个错误:

Manager.obj : error LNK2001: unresolved external symbol "private: static struct Vector Manager::airports" (?airports@Manager@@0U?$Vector@UAirport@@@@A)

Manager.obj : error LNK2001: unresolved external symbol "private: static struct Vector Manager::flights" (?flights@Manager@@0U?$Vector@UFlight@@@@A)

我意识到我需要定义 vector ,但是如何以及在哪里?我尝试创建一个空的构造函数,然后做

Manager::Manager ()
{
vector<string> flights;
vector<string> airports;
}

但它只是给了我一个重新定义的错误。

最佳答案

您必须在 .cpp 文件中定义它们:

vector<string> Manager::flights;
vector<string> Manager::airports;

关于C++ vector 问题 - 'LNK2001: unresolved external symbol private: static...',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6140537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com