StdAfx + Header file - Порядок включения в приложение MFC
Я использую Visual Studio 2005. Я создал консольное приложение на основе MFC под названием «Зависимость StdAfx». IDE создала для меня следующие файлы.
resource.hStdAfx Dependancy.hstdafx.hStdAfx Dependancy.cppstdafx.cppЯ добавил еще один классCHelper
с Helper.h и Helper.cpp, как показано ниже.
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)
{
}
Я создал объект дляCHelper
в основной функции; Для этого я добавил файл Header.h в первую строку StdAfx Dependancy.cpp, как показано ниже; и я получил следующие ошибки.
d: \ codes \ stdafx зависимость \ stdafx зависимость \ stdafx зависимость.cpp (33): ошибка C2065: 'CHelper': необъявленный идентификатор
d: \ codes \ stdafx зависимость \ stdafx зависимость \ stdafx зависимость.cpp (33): ошибка C2146: синтаксическая ошибка: отсутствует ';' перед идентификатором 'myHelper'
d: \ codes \ stdafx зависимость \ stdafx зависимость \ stdafx зависимость.cpp (33): ошибка C2065: 'myHelper': необъявленный идентификатор
Но когда я включаю его послеstdafx.h
, ошибка исчезает. Зачем?
// 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;
}