Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
2.60/5 (2 votes)
See more:
I am getting this undefined reference 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


Here is the code:

C++
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 don't know what this error means and I certainly don't know how to fix it. Any help will be appreciated.
Posted
Updated 27-Nov-13 14:21pm
v2
Comments
Sergey Alexandrovich Kryukov 27-Nov-13 22:05pm    
The source of your problem is not shown in your code sample, as simple as that.
Do this: 1) reduce test application to bare minimum; but it still show the exception; 2) catch all exceptions on the very top stack frame of the thread, 3) find out the whole path of exception propagation; 4) if you cannot resolve the problem, show involved code; or:
1a) or do the same as above but from the debuggert, 2a) see Call Stack (this is the debug window), 3a) if you cannot resolve the problem, show involved code...
—SA

You probably need to define/instantiate your static outside of the class. Since he's independent of instance (static), he needs to be defined outside of the class to actually exist, otherwise he's just been declared (usually you need to do the instantiate inside of the cpp file, if I remember correctly, you're not allowed to do it in a header file).

Same problem as this:
http://stackoverflow.com/questions/272900/undefined-reference-to-static-class-member[^]
 
Share this answer
 
Code looks fine and compiles in vs2010.
Looks like you are missing vector.
You need lines:

C++
#include <vector>;

using namespace std;
 
Share this answer
 
v2
Comments
Philippe Mori 30-Nov-13 9:16am    
The static variable definition is missing and code does not look so fine. You don't want a cast in getCity and you probably want reference (or const reference) for arguments and returns value and in C++, C style cast should not be used (static_cast should be used instead fur numberOfCities function).
Where do you allocate the memory for destinationCities?

You need to initialize destinationCities to a new vector<city> before you can add data to it, otherwise its the same as a null pointer.
 
Share this answer
 
Comments
Heath Branum 27-Nov-13 20:29pm    
But I just need an empty vector. I don't need to put any values in it until addCity function.
Heath Branum 27-Nov-13 20:31pm    
Also I have done the same thing in other classes just fine without any errors. Vectors don't need to be assigned a size because they allocate memory dynamically.
Stefan_Lang 28-Nov-13 5:57am    
Since you declared this member variable as static, you do need to actually declare it elsewhere. it's like declaring a global variable as "extern" in a header without actually defining it anywhere!
Philippe Mori 30-Nov-13 9:13am    
Not exactly the same as a null pointer...
Ron Beyer is right in what he says in solution 1. Any static data members of a class must somewhere be defined. You just need to add a line
C++
vector<city> TourManager::destinationCities;

somewhere in a cpp file. You are correct in assuming that a vector knows how to initialize itself. That applies if you define a vector as an instance member of a class. In your case, however, you are declaring a static members, and all static data members must be defined somewhere. That is the reason it worked in your previous experience and why it didn't in this particular case.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900