FIX: C2248 Error When Calling CView::OnInitialUpdate()

ID: Q113534


The information in this article applies to:
  • The Microsoft Foundation Classes (MFC), included with:
    • Microsoft Visual C++ for Windows, 16-bit edition, version 1.0
    • Microsoft Visual C++, 32-bit Editions, version 1.0


SYMPTOMS

Attempting to call CView::OnInitialUpdate() from a non-CView derived class causes the compiler to generate the following error message:


   error C2248: 'OnInitialUpdate' : cannot access protected member
   declared in class 'CView' 


CAUSE

The CView class contains the function OnInitialUpdate(), which is called by the framework after the view is attached to a document but before the view is initially displayed. OnInitialUpdate() is declared as a protected member of the CView class. Protected member functions cannot be accessed by objects of another class type unless that class is derived from the protected member function's class or is declared to be a friend of the protected member function's class.


STATUS

Microsoft has confirmed this to be a problem in the Microsoft Foundation Classes (MFC) version 2.0. The problem was corrected in MFC version 2.5 and MFC version 3.0.


RESOLUTION

To work around this problem, do one of the following:

  • Upgrade to Visual C++ version 1.5. MFC version 2.5 supplied with Visual C++ version 1.5 for Windows declares the OnInitialUpdate() function as a public member of the CView class.


  • -or-

  • Use the CWnd::SendMessage function to send a WM_INITIALUPDATE message to the view. For example:
    
          class CAnotherClass
          {
              void MemberFunction()
              {
                  CDerivedView * pView = new CDerivedView;
    
                  if( pView != NULL )
                      pView->SendMessage(WM_INITIALUPDATE);
              }
          }; 


  • -or-

  • Derive from CView and declare the class where OnInitialUpdate() is being called from as a friend. For example:
    
          class CDerivedView : public CView
          {
              friend class CAnotherClass;
    
           public:
               virtual void OnDraw(CDC* pDC);
           }; 


  • -or-

  • Derive from CView and override the OnInitialUpdate() member function, making the override a public member of the derived class. For example:
    
          class CDerivedView : public CView
          {
          public:
              virtual void OnDraw(CDC* pDC);
              virtual void OnInitialUpdate(){ CView::OnInitialUpdate(); }
          }; 


Additional query words: 1.00 2.00 2.10 CScrollView CFormView

Keywords : kbDocView kbMFC kbVC
Version : 1.00 | 1.00
Platform : NT WINDOWS
Issue type :


Last Reviewed: September 10, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.