-
May 23rd, 2013, 09:20 AM
#1
Include file semantics?
Compiler error output,
1>MainFrm.cpp
1>z:\. . . . viewswitchview.h(19) : error C2143: syntax error : missing ';' before '*'
1>z:\. . . . viewswitchview.h(19) : error C4430: missing type specifier - int assumed.
1>z:\. . . . viewswitchview.h(19) : error C4430: missing type specifier - int assumed.
1>z:\. . . . viewswitchview.h(19) : warning C4183: 'GetDocument': missing return type;
assumed to be a member function returning 'int'
Comments in code specify where error is and what makes it happen, which is the including one specific include file.
Code:
//=========Here is the MainFrm.cpp file paste=============
// KEEP IN MIND, These errors ONLY occur when I add the
// #include "ViewSwitchView.h" as shown below.
// MainFrm.cpp : implementation of the CMainFrame class
#include "stdafx.h"
#include "ViewSwitch.h"
#include "MainFrm.h"
#ifndef _ViewSwitchView_H_ // Added by me ******************
#include "ViewSwitchView.h" // IF I COMMENT THIS LINE ONLY, PROJECT COMPILES WITH NO ERRORS OR WARNINGS.
#endif //_ViewSwitchView_H_ // SEE THE INCLUDE FILE BELOW AFTER THIS MainFrm.cpp, for error line.
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
END_MESSAGE_MAP()
static UINT indicators[] =
{ ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{// TODO: add member initialization code here
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{ if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{ TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{ TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{ if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
}
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{ CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{ CFrameWnd::Dump(dc);
}
#endif //_DEBUG
// CMainFrame message handlers
// There are none at this time.
//=============End of MainFrm.cpp file===========================
//==============include file paste===============================
// ViewSwitchView.h : interface of the CViewSwitchView class
//
#ifndef _ViewSwitchView_H_
#define _ViewSwitchView_H_ // Note this does not prevent error
#endif //_ViewSwitchView_H_
#pragma once
class CViewSwitchView : public CView
{protected: // create from serialization only
CViewSwitchView();
DECLARE_DYNCREATE(CViewSwitchView)
// Attributes
public:
CViewSwitchDoc* GetDocument() const; //<-- this line (19) flags the error if I include this
// file in MainFrm.cpp, even with the ifndef's above
// Operations
public:
// Overrides
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);
// Implementation
public:
virtual ~CViewSwitchView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
DECLARE_MESSAGE_MAP()
public:
};
#ifndef _DEBUG // debug version in ViewSwitchView.cpp
inline CViewSwitchDoc* CViewSwitchView::GetDocument() const
{ return reinterpret_cast<CViewSwitchDoc*>(m_pDocument); }
#endif
//-----------end of include file---------
-
May 23rd, 2013, 10:03 AM
#2
Re: Include file semantics?
Add forward declaration
Code:
class CViewSwitchDoc;
into ViewSwitchView.h
Victor Nijegorodov
-
May 23rd, 2013, 11:05 AM
#3
Re: Include file semantics?
Originally Posted by VictorN
Add forward declaration
Code:
class CViewSwitchDoc;
into ViewSwitchView.h
Outstanding decipher, the voice of experience speaks.
So then in my novice mind I'm guessing that since #include "ViewSwitchDoc.h" was always in the ViewSwitchView.cpp implementation file, this has kept me from having this before now ?
And now that I'm including the ViewSwitchView.h declaration file somewhere else, it needs the forward declaration to give the compiler a heads up ?
I'm probably just fishing in the wrong ocean on that explanation, but I remember some files I saved on forward declaration information, I will hunt them down and try to educate myself on these ramifications.
Thanks again though, doubt I would have solved this on my own.
-
May 23rd, 2013, 11:12 AM
#4
Re: Include file semantics?
#include guards belong in the header file, not the CPP.
in ViewSwitchView.h
#ifndef _ViewSwitchView_H_
Body of header file goes here
#endif
-
May 23rd, 2013, 11:14 AM
#5
Re: Include file semantics?
Your ViewSwitchView.h contains the declaration of
Code:
CViewSwitchDoc* GetDocument() const;
But compiler while compiling MainFrm.cpp, which also includes ViewSwitchView.h, has no idea what CViewSwitchDoc is.
So you can either add
Code:
#include "ViewSwitchDoc.h"
to MainFrm.cpp (before the
Code:
#include "ViewSwitch.h"
or add the forward declaration into the ViewSwitchDoc.h.
Victor Nijegorodov
-
May 23rd, 2013, 11:57 AM
#6
Re: Include file semantics?
Originally Posted by GCDEF
#include guards belong in the header file, not the CPP.
in ViewSwitchView.h
#ifndef _ViewSwitchView_H_
Body of header file goes here
#endif
I like that even better, now the includers (the cpp files) don't even have to worry with it, since it's covered right in the original header. I've never seen this method before, thanks.
Originally Posted by VictorN
Your ViewSwitchView.h contains the declaration of
Code:
CViewSwitchDoc* GetDocument() const;
But compiler while compiling MainFrm.cpp, which also includes ViewSwitchView.h, has no idea what CViewSwitchDoc is.
So you can either add
Code:
#include "ViewSwitchDoc.h"
to MainFrm.cpp (before the
Code:
#include "ViewSwitch.h"
or add the forward declaration into the ViewSwitchDoc.h.
Ah yes, that zeros in on exactly what's going down while the compiler is focusing on the MainFrm.cpp file. Thanks again.
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
|