moure
moure

Reputation: 1

MFC SDI project want to call GetDocument() function in a view

Now I create a totally new SDI project the view provides a function: GetDocument(), it helps me to get the current document's data

However, When I call the GetDocument() function,VC tells me some error occurs:Debug Assertion Failed

the following is my setting

      class CHorse_programView : public CView
      {
      protected: // create from serialization only
      CHorse_programView();
      DECLARE_DYNCREATE(CHorse_programView)

      // Attributes
      public:
      CHorse_programDoc* GetDocument();

      // Operations
      public:

     // Overrides
     // ClassWizard generated virtual function overrides
     //{{AFX_VIRTUAL(CHorse_programView)
     public:
     virtual void OnDraw(CDC* pDC);  // overridden to draw this view
     virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
     protected:
     virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
     virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
     virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
     //}}AFX_VIRTUAL

     // Implementation
     public:
     virtual ~CHorse_programView();
     CHorse_programDoc * GetDoc()
     {
      CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);
      return (CHorse_programDoc *) pFrame->GetActiveDocument();
     }
     #ifdef _DEBUG
     virtual void AssertValid() const;
     virtual void Dump(CDumpContext& dc) const;
     #endif

     protected:
     // Generated message map functions
     protected:
    //{{AFX_MSG(CHorse_programView)

    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };

and I want to call GetDocument() in this function

    CHorse_programView::CHorse_programView()
    {
      GetDocument();
    }

what's wrong

Upvotes: 0

Views: 609

Answers (2)

ScottMcP-MVP
ScottMcP-MVP

Reputation: 10425

The CDocument and CView are not connected yet at CView construction time. You can move your code to OnInitialUpdate in the view to get full capability.

Upvotes: 1

Mark Ransom
Mark Ransom

Reputation: 308462

In the view's constructor, it hasn't been assigned to a document yet - that comes later.

Upvotes: 0

Related Questions