StdAfx +头文件 - 在MFC应用程序中包含的顺序 [英] StdAfx + Header file - Order of inclusion in MFC application

查看:266
本文介绍了StdAfx +头文件 - 在MFC应用程序中包含的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 2005.我创建了一个名为StdAfx dependancy的基于MFC的控制台应用程序。 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.


  1. Resource.h

  2. StdAfx Dependancy.h

  3. stdafx.h

  4. StdAfx Dependancy.cpp

  5. stdafx.cpp

  1. Resource.h
  2. StdAfx Dependancy.h
  3. stdafx.h
  4. StdAfx Dependancy.cpp
  5. 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

Helper.cpp

#include "StdAfx.h"
#include "Helper.h"

CHelper::CHelper(void)
{
}

CHelper::~CHelper(void)
{
}


$ b b

我在main函数中为 CHelper 创建了一个对象;为了实现这个我添加了Header.h文件在StdAfx Dependancy.cpp的第一行如下;我收到了以下错误。

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\\\\\\\\\\ .cpp(33):
错误C2065:'CHelper':undeclared
标识符

d:\codes\\\\\\\\\\\\\\\\stdafx
dependancy\\\\\\\\\\\\stdafx dependancy\ b $ b dependsancy.cpp(33):error C2146:
语法错误:缺少';'before
标识符'myHelper'

d:\codes\stdafx
dependancy \stdafx dependancy \stdafx
dependsancy.cpp(33):error C2065:
'myHelper':undeclared identifier

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


b $ b

但是当我在 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\n"));
        nRetCode = 1;
    }
    else
    {
        CHelper myHelper;
    }

    return nRetCode;
}


推荐答案

线索。
stdafx.h的用途

编译器忽略
#includestdafx.h之前定义的行。所以如果你想包含这些文件,那么你需要在 #includestdafx.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应用程序中包含的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆