How to use structure constructor ?
-
If I have in header file the follow code :
private:
typedef struct ItemData
{
BOOL bState;
LPCTSTR lpszString;
DWORD dwItemData;
ItemData(LPCTSTR lpszString,BOOL bState,DWORD dwItemData);
virtual ~ItemData();
};and in cpp file I try to use it :
ItemData\* pData = new ItemData(lpszString,TRUE,-1);
I get the follow link error :
error LNK2001: unresolved external symbol "public: __thiscall CComboBoxExt::ItemData::ItemData(char const *,int,unsigned long)" (??0ItemData@CComboBoxExt@@QAE@PBDHK@Z)
What I'm doing wrong ?
-
If I have in header file the follow code :
private:
typedef struct ItemData
{
BOOL bState;
LPCTSTR lpszString;
DWORD dwItemData;
ItemData(LPCTSTR lpszString,BOOL bState,DWORD dwItemData);
virtual ~ItemData();
};and in cpp file I try to use it :
ItemData\* pData = new ItemData(lpszString,TRUE,-1);
I get the follow link error :
error LNK2001: unresolved external symbol "public: __thiscall CComboBoxExt::ItemData::ItemData(char const *,int,unsigned long)" (??0ItemData@CComboBoxExt@@QAE@PBDHK@Z)
What I'm doing wrong ?
You've forgotten to implement functions.
typedef struct ItemData
{
BOOL bState;
LPCTSTR lpszString;
DWORD dwItemData;
ItemData(LPCTSTR lpsz, BOOL bSt, DWORD dw)
: lpszString(lpsz), bState(bSt), dwItemData(dw)
{}
virtual ~ItemData() { };
}; -
You've forgotten to implement functions.
typedef struct ItemData
{
BOOL bState;
LPCTSTR lpszString;
DWORD dwItemData;
ItemData(LPCTSTR lpsz, BOOL bSt, DWORD dw)
: lpszString(lpsz), bState(bSt), dwItemData(dw)
{}
virtual ~ItemData() { };
}; -
Besides, two things: 1. You forgot to provide the typename for your typedef. Eithr delete that typedef keyword or provide the name after the definition of the struct. 2. Do you intend to add types that are derived from ItemData? If not I advise you to remove the virtual destructor! It will only add to the size of the objects because it has to add a hidden pointer to the virtual function table. Virtual destructors are only neccessary to make sure objects are destroyed properly even when the destructor is called through the interface of a base class. But when you have no inheritance it just adds weight unneccessarily.