gpt4 book ai didi

c++ - vector 的 undefined reference

转载 作者:搜寻专家 更新时间:2023-10-31 01:46:45 32 4
gpt4 key购买 nike

我在使用 vector 时得到一个 undefined reference 。

这里是错误:

/tmp/ccYnTr05.o: In function `TourManager::addCity(City)':
tsp.cpp:(.text._ZN11TourManager7addCityE4City[TourManager::addCity(City)]+0x1c): undefined reference to `TourManager::destinationCities'
/tmp/ccYnTr05.o: In function `TourManager::getCity(int)':
tsp.cpp:(.text._ZN11TourManager7getCityEi[TourManager::getCity(int)]+0x14): undefined reference to `TourManager::destinationCities'
/tmp/ccYnTr05.o: In function `TourManager::numberOfCities()':
tsp.cpp:(.text._ZN11TourManager14numberOfCitiesEv[TourManager::numberOfCities()]+0x5): undefined reference to `TourManager::destinationCities'
collect2: ld returned 1 exit status

这是代码片段:

class TourManager
{
private:

static vector<City> destinationCities;

public:

static void addCity(City city)
{
destinationCities.push_back(city);
}

static City getCity(int index)
{
return (City)destinationCities.at(index);
}

static int numberOfCities()
{
return (int)destinationCities.size();
}
};

我意识到vector还没有被初始化为一个值,但是vectors不是动态分配内存吗?我不确定如何解决这个未定义的引用问题?是 vector 问题还是其他问题?谢谢。

最佳答案

您只将 vector 声明为静态数据成员,但您还需要在类外定义它。例如

vector<City> TourManager::destinationCities;

关于c++ - vector 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20256335/

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