使用矢量时,我得到一个未定义的引用。
这是错误:
/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();
}
};
我意识到向量尚未初始化为某个值,但是没有向量动态分配内存吗?我不确定如何修复这个未定义的引用问题?是矢量或其他问题吗?感谢。
答案 0 :(得分:3)
您只将向量声明为静态数据成员,但您还需要在类外部定义它。例如
vector<City> TourManager::destinationCities;