C++ 向量问题 - 'LNK2001:未解析的外部符号私有:静态...'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6140537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:33:17  来源:igfitidea点击:

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

c++

提问by oliverw92

Before anyone calls me out for not looking at pre-existing questions, I have looked and realise that it is to do with declaration, but I still can't get it to work (might be something to do with me using vectors).

在有人因为没有查看预先存在的问题而叫我出去之前,我已经查看并意识到它与声明有关,但我仍然无法让它工作(可能与我使用向量有关)。

Manager.h:

经理.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:

管理器.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;
}

It is throwing this error:

它抛出这个错误:

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)

Manager.obj:错误 LNK2001:未解析的外部符号“私有:静态结构向量管理器::机场”(?airports@Manager@@0U?$Vector@UAirport@@@@A)

Manager.obj:错误 LNK2001:未解析的外部符号“私有:静态结构向量管理器::航班”(?flights@Manager@@0U?$Vector@UFlight@@@@A)

I realise i need to define the vectors, but how and where? I tried creating an empty constructor and then doing

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

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

But it just gave me a re-definition error.

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

回答by Constantinius

You have to define them in the .cppfile:

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

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

回答by Oliver Charlesworth

In your .cpp file, you need to add the definitionsof the static variables:

在您的 .cpp 文件中,您需要添加静态变量的定义

vector<Airport> Manager::airports;
vector<Flight> Manager::flights;

See Why are classes with static data members getting linker errors?from the C++ FAQ.

请参阅为什么具有静态数据成员的类会收到链接器错误?来自 C++ 常见问题解答。

回答by Gavin Lock

Oli and Constantinius have answered your actual question, but I would recommend changing the class. As it stands it has only static members, so you can never create an object of this class. While this is legal C++, it is not in the spirit of C++ (Although there are other languages that do embrace this usage, such as C#)

Oli 和 Constantinius 已经回答了您的实际问题,但我建议更改课程。就目前而言,它只有静态成员,因此您永远无法创建此类的对象。虽然这是合法的 C++,但它不符合 C++ 的精神(尽管还有其他语言确实接受了这种用法,例如 C#)

All static members imply that you are trying for some kind of singleton, but there are more standard ways of enforcing the singleton-ness of a class.

所有静态成员都暗示您正在尝试某种单例,但有更多标准方法可以强制执行类的单例性。

Having said that - I advocate neverbuilding singleton-ness intoa class. Rather write the class as a normal class, and provide a singleton wrapper. This also has the neat side effect of preventing the notorious static initialization order fiascothat your current code is liable to bump into.

话虽如此 - 我主张永远不要将单例性构建一个类中。而是将类编写为普通类,并提供单例包装器。这也有一个巧妙的副作用,可以防止您当前的代码可能遇到的臭名昭著的静态初始化顺序失败

So, something like (excluding the includes for brevity):

因此,类似于(为简洁起见,不包括包含在内):

Manager.h

经理.h

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

Manager& GetManager();

Manager.cpp

管理器.cpp

Manager::Manager()
{
    loadAirports();
    loadFlights();
}

Manager& GetManager()
{
    static Manager manager;
    return manager;
}