I am using Visual Studio 2005. I created an MFC based console application named "StdAfx dependancy". The IDE created the following files for me.
- Resource.h
- StdAfx Dependancy.h
- stdafx.h
- StdAfx Dependancy.cpp
- stdafx.cpp
I added another class CHelper
with Helper.h and Helper.cpp as below.
Helper.h:
#pragma once
class CHelper
{
public:
CHelper(void);
~CHelper(void);
};
Helper.cpp
#include "StdAfx.h"
#include "Helper.h"
CHelper::CHelper(void)
{
}
CHelper::~CHelper(void)
{
}
I created an object for CHelper
in the main function; to achieve this I added Header.h file in the first line of StdAfx Dependancy.cpp as below; and I got the following errors.
d:\codes\stdafx dependancy\stdafx dependancy\stdafx dependancy.cpp(33) : error C2065: 'CHelper' : undeclared identifier d:\codes\stdafx dependancy\stdafx dependancy\stdafx dependancy.cpp(33) : error C2146: syntax error : missing ';' before identifier 'myHelper' d:\codes\stdafx dependancy\stdafx dependancy\stdafx dependancy.cpp(33) : error C2065: 'myHelper' : undeclared identifier
But when I include it after stdafx.h, the error vanishes. Why?
// Stdafx dependancy.cpp : Defines the entry point for the console application.
//
#include "Helper.h"
#include "stdafx.h"
#include "Stdafx dependancy.h"
// #include "Helper.h" --> If I include it here, there is no compilation error
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
CHelper myHelper;
}
return nRetCode;
}
Thanks.