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. 'Forward Class Declaration' Not Working

'Forward Class Declaration' Not Working

Scheduled Pinned Locked Moved C / C++ / MFC
c++debuggingjson
11 Posts 4 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.
  • A Offline
    A Offline
    AmbiguousName
    wrote on last edited by
    #1

    hello guys... I am having a debug assertion when trying to use forward class declaration. Basically I have this CMyTabCtrl inwhich I have forward declared two dialog class, like this: CMyTabCtrl.h

    class CTabOneDlg;
    class CTabTwoDlg;

    And now in the implementaion file, I am using these pointers to create instances of two dialogs and add them to the TabCtrl. Here are the constructor and the InitializeTabs() functions. CMyTabCtrl.cpp

    CMyTabCtrl::CMyTabCtrl()
    {
    m_arrTabs[0] = new CTabOneDlg; //CDialog* m_arrTabs[2];
    m_arrTabs[1] = new CTabTwoDlg;

    m_tabOne = (CTabOneDlg*)m_arrTabs[0]; // CTabOneDlg* m_tabOne;
    m_tabTwo = (CTabTwoDlg*)m_arrTabs[1]; // CTabTwoDlg* m_tabTwo;
    }

    coid CMyTabCtrl::InitializeTabs()
    {
    this->InsertItem(0, _T("Faculty"));
    this->InsertItem(1, _T("Admin"));

    //create the dialogs
    m\_arrTabs\[0\]->Create(IDD\_TAB1\_DLG, this);
    m\_arrTabs\[1\]->Create(IDD\_TAB2\_DLG, this);
    
    //show the firs and hide the rest
    m\_arrTabs\[0\]->ShowWindow(SW\_SHOW);
    m\_arrTabs\[1\]->ShowWindow(SW\_HIDE);
    

    }

    And finally when in MainDialogDlg.cpp, I try to forward declare this CMyTabCtrl class, it gives debug assertion on the second line of the InitializeTabs() function. CMainDialogDlg.h

    class CMyTabCtrl;

    CMyTabCtrl *tabCtrl;

    CMainDialogDlg.cpp

    BOOL CMainDialogDlg::OnInitDialog()
    {
    tabCtrl = new CMyTabCtrl;
    tabCtrl->InitizlizeTabs();
    }

    The debug assertion it gives is as follows

    mfc100ud.dll!CTabCtrl::InsertItem(int nItem, const wchar_t * lpszItem) Line 570 + 0x2d bytes C++

    Why it is giving debug assertion on the InsertItem() function in the InitializeTabs() function. Thanks for at least giving time to read it.

    This world is going to explode due to international politics, SOON.

    _ L K 3 Replies Last reply
    0
    • A AmbiguousName

      hello guys... I am having a debug assertion when trying to use forward class declaration. Basically I have this CMyTabCtrl inwhich I have forward declared two dialog class, like this: CMyTabCtrl.h

      class CTabOneDlg;
      class CTabTwoDlg;

      And now in the implementaion file, I am using these pointers to create instances of two dialogs and add them to the TabCtrl. Here are the constructor and the InitializeTabs() functions. CMyTabCtrl.cpp

      CMyTabCtrl::CMyTabCtrl()
      {
      m_arrTabs[0] = new CTabOneDlg; //CDialog* m_arrTabs[2];
      m_arrTabs[1] = new CTabTwoDlg;

      m_tabOne = (CTabOneDlg*)m_arrTabs[0]; // CTabOneDlg* m_tabOne;
      m_tabTwo = (CTabTwoDlg*)m_arrTabs[1]; // CTabTwoDlg* m_tabTwo;
      }

      coid CMyTabCtrl::InitializeTabs()
      {
      this->InsertItem(0, _T("Faculty"));
      this->InsertItem(1, _T("Admin"));

      //create the dialogs
      m\_arrTabs\[0\]->Create(IDD\_TAB1\_DLG, this);
      m\_arrTabs\[1\]->Create(IDD\_TAB2\_DLG, this);
      
      //show the firs and hide the rest
      m\_arrTabs\[0\]->ShowWindow(SW\_SHOW);
      m\_arrTabs\[1\]->ShowWindow(SW\_HIDE);
      

      }

      And finally when in MainDialogDlg.cpp, I try to forward declare this CMyTabCtrl class, it gives debug assertion on the second line of the InitializeTabs() function. CMainDialogDlg.h

      class CMyTabCtrl;

      CMyTabCtrl *tabCtrl;

      CMainDialogDlg.cpp

      BOOL CMainDialogDlg::OnInitDialog()
      {
      tabCtrl = new CMyTabCtrl;
      tabCtrl->InitizlizeTabs();
      }

      The debug assertion it gives is as follows

      mfc100ud.dll!CTabCtrl::InsertItem(int nItem, const wchar_t * lpszItem) Line 570 + 0x2d bytes C++

      Why it is giving debug assertion on the InsertItem() function in the InitializeTabs() function. Thanks for at least giving time to read it.

      This world is going to explode due to international politics, SOON.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      In which files have you #included the tab dialog and tab control headers?

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++)

      Polymorphism in C

      A 1 Reply Last reply
      0
      • A AmbiguousName

        hello guys... I am having a debug assertion when trying to use forward class declaration. Basically I have this CMyTabCtrl inwhich I have forward declared two dialog class, like this: CMyTabCtrl.h

        class CTabOneDlg;
        class CTabTwoDlg;

        And now in the implementaion file, I am using these pointers to create instances of two dialogs and add them to the TabCtrl. Here are the constructor and the InitializeTabs() functions. CMyTabCtrl.cpp

        CMyTabCtrl::CMyTabCtrl()
        {
        m_arrTabs[0] = new CTabOneDlg; //CDialog* m_arrTabs[2];
        m_arrTabs[1] = new CTabTwoDlg;

        m_tabOne = (CTabOneDlg*)m_arrTabs[0]; // CTabOneDlg* m_tabOne;
        m_tabTwo = (CTabTwoDlg*)m_arrTabs[1]; // CTabTwoDlg* m_tabTwo;
        }

        coid CMyTabCtrl::InitializeTabs()
        {
        this->InsertItem(0, _T("Faculty"));
        this->InsertItem(1, _T("Admin"));

        //create the dialogs
        m\_arrTabs\[0\]->Create(IDD\_TAB1\_DLG, this);
        m\_arrTabs\[1\]->Create(IDD\_TAB2\_DLG, this);
        
        //show the firs and hide the rest
        m\_arrTabs\[0\]->ShowWindow(SW\_SHOW);
        m\_arrTabs\[1\]->ShowWindow(SW\_HIDE);
        

        }

        And finally when in MainDialogDlg.cpp, I try to forward declare this CMyTabCtrl class, it gives debug assertion on the second line of the InitializeTabs() function. CMainDialogDlg.h

        class CMyTabCtrl;

        CMyTabCtrl *tabCtrl;

        CMainDialogDlg.cpp

        BOOL CMainDialogDlg::OnInitDialog()
        {
        tabCtrl = new CMyTabCtrl;
        tabCtrl->InitizlizeTabs();
        }

        The debug assertion it gives is as follows

        mfc100ud.dll!CTabCtrl::InsertItem(int nItem, const wchar_t * lpszItem) Line 570 + 0x2d bytes C++

        Why it is giving debug assertion on the InsertItem() function in the InitializeTabs() function. Thanks for at least giving time to read it.

        This world is going to explode due to international politics, SOON.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        This has nothing to do with forward declaration; that is merely a convenience for the compiler. You should look more closely at the actual code, and use your debugger to step through and try to see which variable is causing the actual assertion.

        Programming is work, it isn't finger painting. Luc Pattyn

        A 1 Reply Last reply
        0
        • _ _Superman_

          In which files have you #included the tab dialog and tab control headers?

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++)

          Polymorphism in C

          A Offline
          A Offline
          AmbiguousName
          wrote on last edited by
          #4

          Tab Dialog are #included in CMyTabCtrl.cpp like this CMyTabCtrl.h

          class CTabOneDlg;
          class CTabTwoDlg;

          CMyTabCtrl.cpp (Constructor)

          m_arrTabs[0] = new CTabOneDlg; //CDialog* m_arrTabs[2];
          m_arrTabs[1] = new CTabTwoDlg;

          While the class for Tab control is #included in main dialog like this. CMainDialogDlg.h

          class CMyTabCtrl;

          public:
          CMyTabCtrl *tabCtrl;

          CMainDialogDlg.cpp (OnInitDialog)

          tabCtrl = new CMyTabCtrl;
          tabCtrl->InitizlizeTabs();
          

          BTW, this application works fine if I dont use forward decaration method and #include all the files, normally the way we do.

          This world is going to explode due to international politics, SOON.

          1 Reply Last reply
          0
          • L Lost User

            This has nothing to do with forward declaration; that is merely a convenience for the compiler. You should look more closely at the actual code, and use your debugger to step through and try to see which variable is causing the actual assertion.

            Programming is work, it isn't finger painting. Luc Pattyn

            A Offline
            A Offline
            AmbiguousName
            wrote on last edited by
            #5

            Yes. I did exactly that. Everything works fine if I #include all the header files normally. The variable that acrually causing the problem is tabCtrl. It gives debug assertion in the second line of the InitializeTabs() function. The assertion it gives is as follows.

            mfc100ud.dll!CTabCtrl::InsertItem(int nItem, const wchar_t * lpszItem) Line 570 + 0x2d bytes C++

            And if you see the second line of the the said function then you will notice that it is merely inserting the tab dialog at the specified location like this

            this->InsertItem(0, _T("Faculty"));
            this->InsertItem(1, _T("Admin"));

            So this should not be a problme I think.

            This world is going to explode due to international politics, SOON.

            L 1 Reply Last reply
            0
            • A AmbiguousName

              Yes. I did exactly that. Everything works fine if I #include all the header files normally. The variable that acrually causing the problem is tabCtrl. It gives debug assertion in the second line of the InitializeTabs() function. The assertion it gives is as follows.

              mfc100ud.dll!CTabCtrl::InsertItem(int nItem, const wchar_t * lpszItem) Line 570 + 0x2d bytes C++

              And if you see the second line of the the said function then you will notice that it is merely inserting the tab dialog at the specified location like this

              this->InsertItem(0, _T("Faculty"));
              this->InsertItem(1, _T("Admin"));

              So this should not be a problme I think.

              This world is going to explode due to international politics, SOON.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Overloaded_Name wrote:

              It gives debug assertion

              Yes, but what assertion; what is the value (or missing value) that actually causes the assertion?

              Programming is work, it isn't finger painting. Luc Pattyn

              A 1 Reply Last reply
              0
              • L Lost User

                Overloaded_Name wrote:

                It gives debug assertion

                Yes, but what assertion; what is the value (or missing value) that actually causes the assertion?

                Programming is work, it isn't finger painting. Luc Pattyn

                A Offline
                A Offline
                AmbiguousName
                wrote on last edited by
                #7

                It just takes me to this line in afxcmn.inl

                { ASSERT(::IsWindow(m\_hWnd)); return CTabCtrl::InsertItem(TCIF\_TEXT, nItem, lpszItem, 0, 0); }
                

                If I see in the locals then I find these values

                this 0x0019fc18 {CMyTabCtrl hWnd=0x00000000} CTabCtrl * const
                nItem 0 int

                If there is something else I should look for, then tell me.

                This world is going to explode due to international politics, SOON.

                L 1 Reply Last reply
                0
                • A AmbiguousName

                  It just takes me to this line in afxcmn.inl

                  { ASSERT(::IsWindow(m\_hWnd)); return CTabCtrl::InsertItem(TCIF\_TEXT, nItem, lpszItem, 0, 0); }
                  

                  If I see in the locals then I find these values

                  this 0x0019fc18 {CMyTabCtrl hWnd=0x00000000} CTabCtrl * const
                  nItem 0 int

                  If there is something else I should look for, then tell me.

                  This world is going to explode due to international politics, SOON.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  That clearly shows that CMyTabCtrl has not been created properly, as it does not have a Window associated with it. You need to look at where you create it to try to discover why the create is failing, or why it has not been called properly.

                  Programming is work, it isn't finger painting. Luc Pattyn

                  1 Reply Last reply
                  0
                  • A AmbiguousName

                    hello guys... I am having a debug assertion when trying to use forward class declaration. Basically I have this CMyTabCtrl inwhich I have forward declared two dialog class, like this: CMyTabCtrl.h

                    class CTabOneDlg;
                    class CTabTwoDlg;

                    And now in the implementaion file, I am using these pointers to create instances of two dialogs and add them to the TabCtrl. Here are the constructor and the InitializeTabs() functions. CMyTabCtrl.cpp

                    CMyTabCtrl::CMyTabCtrl()
                    {
                    m_arrTabs[0] = new CTabOneDlg; //CDialog* m_arrTabs[2];
                    m_arrTabs[1] = new CTabTwoDlg;

                    m_tabOne = (CTabOneDlg*)m_arrTabs[0]; // CTabOneDlg* m_tabOne;
                    m_tabTwo = (CTabTwoDlg*)m_arrTabs[1]; // CTabTwoDlg* m_tabTwo;
                    }

                    coid CMyTabCtrl::InitializeTabs()
                    {
                    this->InsertItem(0, _T("Faculty"));
                    this->InsertItem(1, _T("Admin"));

                    //create the dialogs
                    m\_arrTabs\[0\]->Create(IDD\_TAB1\_DLG, this);
                    m\_arrTabs\[1\]->Create(IDD\_TAB2\_DLG, this);
                    
                    //show the firs and hide the rest
                    m\_arrTabs\[0\]->ShowWindow(SW\_SHOW);
                    m\_arrTabs\[1\]->ShowWindow(SW\_HIDE);
                    

                    }

                    And finally when in MainDialogDlg.cpp, I try to forward declare this CMyTabCtrl class, it gives debug assertion on the second line of the InitializeTabs() function. CMainDialogDlg.h

                    class CMyTabCtrl;

                    CMyTabCtrl *tabCtrl;

                    CMainDialogDlg.cpp

                    BOOL CMainDialogDlg::OnInitDialog()
                    {
                    tabCtrl = new CMyTabCtrl;
                    tabCtrl->InitizlizeTabs();
                    }

                    The debug assertion it gives is as follows

                    mfc100ud.dll!CTabCtrl::InsertItem(int nItem, const wchar_t * lpszItem) Line 570 + 0x2d bytes C++

                    Why it is giving debug assertion on the InsertItem() function in the InitializeTabs() function. Thanks for at least giving time to read it.

                    This world is going to explode due to international politics, SOON.

                    K Offline
                    K Offline
                    krmed
                    wrote on last edited by
                    #9

                    Since the assertion indicates that the mhWnd is NULL, the problem lies in this area:

                    Overloaded_Name wrote:

                    BOOL CMainDialogDlg::OnInitDialog() { tabCtrl = new CMyTabCtrl; tabCtrl->InitizlizeTabs(); }

                    The first line creates an instance of the class, but does not create the window associated with it. The second line tries to insert information into the window - but there is none. As Richard MacCutchen said, you need to Create the tab control. Hope this helps.

                    Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

                    L A 2 Replies Last reply
                    0
                    • K krmed

                      Since the assertion indicates that the mhWnd is NULL, the problem lies in this area:

                      Overloaded_Name wrote:

                      BOOL CMainDialogDlg::OnInitDialog() { tabCtrl = new CMyTabCtrl; tabCtrl->InitizlizeTabs(); }

                      The first line creates an instance of the class, but does not create the window associated with it. The second line tries to insert information into the window - but there is none. As Richard MacCutchen said, you need to Create the tab control. Hope this helps.

                      Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Well spotted, I failed to notice this obvious mistake.

                      Programming is work, it isn't finger painting. Luc Pattyn

                      1 Reply Last reply
                      0
                      • K krmed

                        Since the assertion indicates that the mhWnd is NULL, the problem lies in this area:

                        Overloaded_Name wrote:

                        BOOL CMainDialogDlg::OnInitDialog() { tabCtrl = new CMyTabCtrl; tabCtrl->InitizlizeTabs(); }

                        The first line creates an instance of the class, but does not create the window associated with it. The second line tries to insert information into the window - but there is none. As Richard MacCutchen said, you need to Create the tab control. Hope this helps.

                        Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

                        A Offline
                        A Offline
                        AmbiguousName
                        wrote on last edited by
                        #11

                        krmed wrote:

                        The first line creates an instance of the class, but does not create the window associated with it.

                        Ok. I understand where the problem lies. But that's what I stated in my OP. If I do this application normally, like #including the tab files in the Tab Ctrl header file and then, #including this tab cntrl header file in the main dialog header file. And finally when I do

                        CMyTabCtrl tabCtrl;
                        tabCtrl.InitializeTabs();

                        It works perfectly. However thank you for your time. I am trying to do this, in this way as well.

                        This world is going to explode due to international politics, SOON.

                        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