4

I am getting an undefined reference while using vectors.

Here is the error:

/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

And here is the code snippet:

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();
    }
};

I realize that the vector has not been initialized to a value, but don't vectors dynamically allocate memory? I am not sure how to fixed this undefined reference issue? Is the problem with the vector or something else? Thanks.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
freecrap
  • 41
  • 1
  • 2

1 Answers1

7

You only declared the vector as a static data member but you also need to define it outside the class. For example

vector<City> TourManager::destinationCities;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335