-
November 29th, 2013, 02:19 PM
#1
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.
-
November 29th, 2013, 02:35 PM
#2
Re: How to get pointer to CMyView class from CMainFrame class?
Originally Posted by coderDude1
... 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
-
November 29th, 2013, 02:35 PM
#3
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)
-
November 29th, 2013, 03:13 PM
#4
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;
}
-
November 29th, 2013, 03:31 PM
#5
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
-
November 29th, 2013, 03:49 PM
#6
Re: How to get pointer to CMyView class from CMainFrame class?
Originally Posted by coderDude1
...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...
-
November 29th, 2013, 09:08 PM
#7
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.
-
December 2nd, 2013, 02:52 AM
#8
Re: How to get pointer to CMyView class from CMainFrame class?
Originally Posted by coderDude1
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
-
December 2nd, 2013, 08:06 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|