StdAfx + Header file - Order of inclusion in MFC application(StdAfx + 头文件 - MFC 应用程序中包含的顺序)
问题描述
我正在使用 Visual Studio 2005.我创建了一个基于 MFC 的控制台应用程序,名为StdAfx 依赖".IDE 为我创建了以下文件.
I am using Visual Studio 2005. I created an MFC based console application named "StdAfx dependancy". The IDE created the following files for me.
- 资源.h
- StdAfx Dependancy.h
- stdafx.h
- StdAfx Dependancy.cpp
- stdafx.cpp
我用 Helper.h 和 Helper.cpp 添加了另一个类 CHelper,如下所示.
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)
{
}
我在主函数中为 CHelper 创建了一个对象;为了实现这一点,我在 StdAfx Dependancy.cpp 的第一行添加了 Header.h 文件,如下所示;我收到以下错误.
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:codesstdafx 依赖关系stdafx依赖stdafx 依赖.cpp(33) :错误 C2065:'CHelper':未声明标识符
d:代码stdafx依赖stdafx 依赖stdafxdependancy.cpp(33):错误 C2146:语法错误:缺少 ';'前标识符myHelper"
d:代码stdafx依赖stdafx 依赖stdafxdependancy.cpp(33):错误 C2065:'myHelper' : 未声明的标识符
d:codesstdafx dependancystdafx dependancystdafx dependancy.cpp(33) : error C2065: 'CHelper' : undeclared identifier
d:codesstdafx dependancystdafx dependancystdafx dependancy.cpp(33) : error C2146: syntax error : missing ';' before identifier 'myHelper'
d:codesstdafx dependancystdafx dependancystdafx dependancy.cpp(33) : error C2065: 'myHelper' : undeclared identifier
但是当我在 stdafx.h 之后包含它时,错误消失了.为什么?
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
"));
nRetCode = 1;
}
else
{
CHelper myHelper;
}
return nRetCode;
}
推荐答案
这个链接一定能给你一些线索.stdafx.h的目的
This link must give you some clue. Purpose of stdafx.h
之前定义的行#include "stdafx.h" 被编译器忽略.因此,如果您想实际包含这些文件,则需要在 #include "stdafx.h" 之后包含它们.
The lines defined before the
#include "stdafx.h" are ignored by the compiler. So if you want to actually include those files then you need to include them after the #include "stdafx.h".
希望清楚.
这篇关于StdAfx + 头文件 - MFC 应用程序中包含的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:StdAfx + 头文件 - MFC 应用程序中包含的顺序
基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
