static class members and link error LNK2001

G

Guest

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.
System WIN M

class CCit

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

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


class CDataInput : public CDialo

public
CDataInput(CWnd* pParent /*= NULL*/); // standard
CWnd* m_pParent
static CityVector theVector
..


void CDataInput::OnAdd()

UpdateData(TRUE)
//store the coordinate data in an CMap objec
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 vecto
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@@@std@@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@@@std@@A
Debug/SplitWnd.exe : fatal error LNK1120: 1 unresolved external
Error executing link.exe

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

David Lowndes

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?
<

Add a line to the source module:

CityVector CDataInput::theVector;

to create the static variable instance.

Dave
 
G

Guest

Dave

Thank you very much indeed. It works!
I think the previous mistake of the code is a lack of a valid defination of the static member theVector.
 

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