Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to use structure constructor ?

How to use structure constructor ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    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 ?

    O 1 Reply Last reply
    0
    • _ _Flaviu

      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 ?

      O Offline
      O Offline
      Ozer Karaagac
      wrote on last edited by
      #2

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

      _ 1 Reply Last reply
      0
      • O Ozer Karaagac

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

        _ Offline
        _ Offline
        _Flaviu
        wrote on last edited by
        #3

        You are perfectly right ! Thank you !

        S 1 Reply Last reply
        0
        • _ _Flaviu

          You are perfectly right ! Thank you !

          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups