1
我在使用矢量時收到未定義的參考。矢量的未定義參考
以下是錯誤:
/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();
}
};
我意識到矢量尚未初始化爲一個值,但不向量動態分配記憶?我不知道如何解決這個未定義的參考問題?是矢量還是別的問題?謝謝。
當我這樣做時,我得到錯誤:「成員'destinationCities的額外資格。'」 – freecrap
另外一個你應該在類定義之外做的。 –