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!
Re: How to get pointer to CMyView class from CMainFrame class?
Quote:
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?
Re: How to get pointer to CMyView class from CMainFrame class?
Quote:
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 '#'.
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;
}
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?
Re: How to get pointer to CMyView class from CMainFrame class?
Quote:
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.
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.
Re: How to get pointer to CMyView class from CMainFrame class?
Quote:
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
// ...
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.