我在CMainFrame里添加了一个菜单事件,一点就调用View类的ShowMessage()方法,我试了试不行,总出错.
void CSDIView::ShowMessage()
{
MessageBox("zhengjia!!!");
}
void CMainFrame::OnOoTest() 
{
// TODO: Add your command handler code here

CSDIView::ShowMessage();
}
各位指点一下

解决方案 »

  1.   

    先得到view 指针
    GetActiveView
      

  2.   

    一般性,OnOoTest() 是在doc里面实现的。
      

  3.   

    goldenfaith(bird) 可否给出详细代码?我刚接触VC
      

  4.   

    CSDIView* pView = (CSDIView*)GetActiveView();
    pView->ShowMessage();
      

  5.   

    SoLike(是我) :
    你的方法我试过,不行;
    --------------------Configuration: SDI - Win32 Debug--------------------
    Compiling...
    MainFrm.cpp
    E:\VC_EXE\SDI\MainFrm.cpp(112) : error C2065: 'CSDIView' : undeclared identifier
    E:\VC_EXE\SDI\MainFrm.cpp(112) : error C2065: 'pView' : undeclared identifier
    E:\VC_EXE\SDI\MainFrm.cpp(112) : error C2059: syntax error : ')'
    E:\VC_EXE\SDI\MainFrm.cpp(113) : error C2227: left of '->ShowMessage' must point to class/struct/union
    Error executing cl.exe.SDI.exe - 4 error(s), 0 warning(s)
      

  6.   

    晕,不会连这点编译错误还要请教人家吧??
    在class CMainFrame类定义前加Class CSDIView;或者#include "SDIView.h"
      

  7.   

    liushax():
    加头文件我也试过了,出错:
    -------------------Configuration: SDI - Win32 Debug--------------------
    Compiling...
    MainFrm.cpp
    e:\vc_exe\sdi\sdiview.h(21) : error C2143: syntax error : missing ';' before '*'
    e:\vc_exe\sdi\sdiview.h(21) : error C2501: 'CSDIDoc' : missing storage-class or type specifiers
    e:\vc_exe\sdi\sdiview.h(21) : error C2501: 'GetDocument' : missing storage-class or type specifiers
    E:\VC_EXE\SDI\MainFrm.cpp(114) : error C2248: 'ShowMessage' : cannot access protected member declared in class 'CSDIView'
            e:\vc_exe\sdi\sdiview.h(37) : see declaration of 'ShowMessage'
    Error executing cl.exe.SDI.exe - 4 error(s), 0 warning(s)
      

  8.   

    去掉头文件,在class CMainFrame类定义前加Class CSDIView,出错:
    --------------------Configuration: SDI - Win32 Debug--------------------
    Compiling...
    MainFrm.cpp
    E:\VC_EXE\SDI\MainFrm.cpp(37) : error C2146: syntax error : missing ';' before identifier 'CSDIView'
    E:\VC_EXE\SDI\MainFrm.cpp(37) : error C2501: 'Class' : missing storage-class or type specifiers
    E:\VC_EXE\SDI\MainFrm.cpp(37) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.
      

  9.   

    [email protected],是否介意发过来?
      

  10.   

    我也遇到这样的问题,你试试这样:
    在CYourApp中添加各种视或文档的指针,在那些视或文档初始化的时候将指针传给CYourApp中的对应变量。这样以后不管在哪用上面指针只需(CYourApp*)AfxGetApp()取其属性变量即可。
    如:
        CMyApp* pApp=(CMyApp*) AfxGetApp();
        pApp->pView->ShowMessage();
    试试这样,看怎么样。
      

  11.   

    liushax() :
    跟本不用发,我通过向导建立了一个SDI工程,什么都没动,就加了这么两个函数,你建个试试
      

  12.   

    1: 在MainFrm.cpp中加#include "SDIView.h",
     2: 在CSDIView类定义前加class CSDIDoc;
     3: MainFrm.cpp类中实现如下
      void CMainFrame::OnOoTest() 
     {
    CTempMdiView *pView = (CTempMdiView*)GetActiveView();
    pView->ShowMessage();
      }
      不用我再说了吧
      

  13.   

    liushax() :我照你的做了,但:
    --------------------Configuration: SDI - Win32 Debug--------------------
    Compiling...
    MainFrm.cpp
    E:\VC_EXE\SDI\MainFrm.cpp(112) : error C2248: 'ShowMessage' : cannot access protected member declared in class 'CSDIView'
            e:\vc_exe\sdi\sdiview.h(38) : see declaration of 'ShowMessage'
    Error executing cl.exe.SDI.exe - 1 error(s), 0 warning(s)
    我的ShowMessage是这样声明的:
    class CSDIView : public CView
    {
    protected: // create from serialization only
    CSDIView();
    DECLARE_DYNCREATE(CSDIView)// Attributes
    public:
    CSDIDoc* GetDocument();// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CSDIView)
    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
    void ShowMessage();
    后面省略....
    不明白了
      

  14.   

    你的ShowMessage();是protected:类型
    当然会出错啦
    建议你先看看C++的书吧。
      

  15.   

    哦,我没有看清楚,这次可以了,但是方法是不是太繁琐啦,为什么在CSDIView类定义前加class CSDIDoc呢????
      

  16.   

    在MainFrm.cpp中加#include "***Doc.h"  ;
    在MainFrm.cpp中加#include "***View.h" ;注意头文件的顺序