static class members and link error LNK2001

J

Jiaxin Deng

Please help me out of this.
The purpose of the following code is to store city objects in a
vector. And to make the vector independent of the existense of the
dialog, it is declared as a static member. The code compiles without
problem. However, it incurs a LNK2001 error when linking. Will be
grateful if you tell me why and how to solve this problem?

Compiler VC6.0
System WIN ME

class CCity
{
public:
CCity(){};
~CCity(){};
int CityID;
double m_Longitude;
double m_Latitude;
};

#include <vector>
using namespace std ;
typedef std::vector<CCity> CityVector;



class CDataInput : public CDialog
{
public:
CDataInput(CWnd* pParent /*= NULL*/); // standard
CWnd* m_pParent;
static CityVector theVector;
...
}

void CDataInput::OnAdd()
{
UpdateData(TRUE);
//store the coordinate data in an CMap object
CCity NewCity;
NewCity.m_Longitude = m_CoordX;
NewCity.m_Latitude = m_CoordY;
NewCity.CityID++;
NewCity.CityID = nCounter;
nCounter++;

// Append one element to the end of the vector
theVector.push_back(NewCity);
}

--------------------Configuration: SplitWnd - Win32
Debug--------------------
Linking...
DataInput.obj : error LNK2001: unresolved external symbol "public:
static class std::vector<class CCity,class std::allocator<class CCity> >
CDataInput::theVector"
(?theVector@CDataInput@@2V?$vector@VCCity@@V?$allocator@VCCity@@@std@@@s
td@@A)
SplitWndDoc.obj : error LNK2001: unresolved external symbol "public:
static class std::vector<class CCity,class std::allocator<class CCity> >
CDataInput::theVector"
(?theVector@CDataInput@@2V?$vector@VCCity@@V?$allocator@VCCity@@@std@@@s
td@@A)
Debug/SplitWnd.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

SplitWnd.exe - 3 error(s), 0 warning(s)
 
P

Polaris

The static member must be initialized at a global scope. Try to initialize
it like this:

CityVector CDataInput::theVector;

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top