【问题标题】:MFC SDI project want to call GetDocument() function in a viewMFC SDI 项目要在视图中调用 GetDocument() 函数
【发布时间】:2014-08-05 10:52:59
【问题描述】:

现在我创建了一个全新的 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();
    }

怎么了

【问题讨论】:

    标签: mfc sdi


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 感谢您的回复
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多