CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  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 02:24 PM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  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
    Hanover Germany
    Posts
    20,395

    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,656

    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:
    FeinWindows - replacement windows manager for Visual Studio, 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
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    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 02:56 AM. Reason: typos
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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
  •  





Click Here to Expand Forum to Full Width

Featured