0

现在我创建了一个全新的 SDI 项目,视图提供了一个函数:GetDocument(),它可以帮助我获取当前文档的数据

但是,当我调用 GetDocument() 函数时,VC 告诉我发生了一些错误:Debug Assertion Failed

以下是我的设置

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

我想在这个函数中调用 GetDocument()

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

怎么了

4

2 回答 2

1

CDocument 和 CView 在 CView 构建时尚未连接。您可以将代码移动到视图中的 OnInitialUpdate 以获得完整功能。

于 2014-08-05T02:52:59.530 回答
0

在视图的构造函数中,它还没有被分配给一个文档——稍后会出现。

于 2014-08-05T02:52:41.400 回答