How to get pointer to CMyView class from CMainFrame class?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: How to get pointer to CMyView class from CMainFrame class?

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    5

    How to get pointer to CMyView class from CMainFrame class?

    My program is a basic MFC AppWizard (exe) created project in VC++ 6.
    In MainFrm.cpp, I am trying to access some user defined CMyView member functions.
    However when I try to do the standard procedure to get the CMyView pointer in MainFrm.cpp, I get the " ... 'CMyView' : undeclared identifier" error. To resolve this, I add " #include myView.h " at the top of MainFrm.h which then produces the following errors:

    Code:
    myview.h(21) : error C2143: syntax error : missing ';' before '*'
    myview.h(21) : error C2501: 'CMyDoc' : missing storage-class or type specifiers
    myview.h(21) : error C2501: 'GetDocument' : missing storage-class or type specifiers
    What do these errors mean? Is there a simple way to access CMyView member functions from CMainFrame? Please help!
    Last edited by coderDude1; November 29th, 2013 at 01:24 PM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,477

    Re: How to get pointer to CMyView class from CMainFrame class?

    Quote Originally Posted by coderDude1 View Post
    ... Is there a simple way to access CMyView member functions from CMainFrame? Please help!
    Well it could be possible sometimes. but in general it is not a good idea.
    Why do you think you need it?
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,569

    Re: How to get pointer to CMyView class from CMainFrame class?

    What do these errors mean?
    For help with C2143 see
    http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx

    if you posted the code we could provide more guidance. When posting code, please format it properly first before posting and use code tags. Go Advanced, select the code and click '#'.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Nov 2013
    Posts
    5

    Re: How to get pointer to CMyView class from CMainFrame class?

    I need to access CMyView member functions from CMainFrame to address another issue.
    The issue is when I loose focus from my main view (for example clicking on a dialog button), the CMyView::OnMouseWheel() function from the WM_MOUSEWHEEL message does not work properly until the focus is returned to the main view (with a click). This issue is resolved by implementing CMainFrame::OnMouseWheel() in CMainFrame. However, I need additional functionality that is contained in CMyView member functions.

    Therefore I am trying to access CMyView from CMainFrame. Is it possible?

    The piece of the code that I am having trouble with is equivalent to adding a simple tester() function in a blank MFC AppWizard (exe) created project:
    Code:
    void CMainFrame::tester()
    {
         CMyView * pMyView;
    }

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,477

    Re: How to get pointer to CMyView class from CMainFrame class?

    Is your project a SDI or MDI?
    Why do you need the pointer to CMyView class? isn't the pointer to the base CView class enough?
    Victor Nijegorodov

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,527

    Re: How to get pointer to CMyView class from CMainFrame class?

    Quote Originally Posted by coderDude1 View Post
    ...What do these errors mean? Is there a simple way to access CMyView member functions from CMainFrame? Please help!
    This error means that your View class is using your Document class, without including its header in the myView.h (possibly relying that it will get included first in the cpp file that uses it).
    Simply including the doc header right before the view header will fix this problem.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  7. #7
    Join Date
    Nov 2013
    Posts
    5

    Re: How to get pointer to CMyView class from CMainFrame class?

    Thank you very much for the help!

    Indeed the AppWizard created the myView.cpp with a "#include myDoc.h" line. I moved "#include myDoc.h" to the myView.h file, and was then able to use "#include myView.h" line in the MainFrm.h file without errors.

    As a result, I can access the CMyView pointer in CMainFrame class.

  8. #8
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,051

    Re: How to get pointer to CMyView class from CMainFrame class?

    Quote Originally Posted by coderDude1 View Post
    Thank you very much for the help!

    Indeed the AppWizard created the myView.cpp with a "#include myDoc.h" line. I moved "#include myDoc.h" to the myView.h file, and was then able to use "#include myView.h" line in the MainFrm.h file without errors.

    As a result, I can access the CMyView pointer in CMainFrame class.
    Although it may work, it's not a very good idea of moving and/or swapping header includes each time something is going wrong.
    If deal with pointers and/or references, much better, safer and easier is to make forward declarations.

    Example
    Code:
    class CMyDoc; // forward declaration
    class CMyView : public CView
    {
       // ... 
       CMyDoc* GetDocument() const; // OK, regardless the place and order of MyDoc.h include
       // ...
    Last edited by ovidiucucu; December 2nd, 2013 at 01:56 AM. Reason: typos
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,994

    Re: How to get pointer to CMyView class from CMainFrame class?

    if the view is the active view:
    CFrameWnd::GetActiveView(), test the result with IsKindOf() and cast to the view type you need.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center