【问题标题】:StdAfx + Header file - Order of inclusion in MFC applicationStdAfx + 头文件 - MFC 应用程序中包含的顺序
【发布时间】:2011-03-06 15:37:00
【问题描述】:

我正在使用 Visual Studio 2005。我创建了一个基于 MFC 的控制台应用程序,名为“StdAfx 依赖”。 IDE 为我创建了以下文件。

  1. 资源.h
  2. StdAfx Dependancy.h
  3. stdafx.h
  4. StdAfx Dependancy.cpp
  5. stdafx.cpp

我用 Helper.h 和 Helper.cpp 添加了另一个类 CHelper,如下所示。

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 文件,如下所示;我收到以下错误。

d:\codes\stdafx 依赖关系\stdafx 依赖\stdafx 依赖.cpp(33) : 错误 C2065:'CHelper':未声明 标识符
d:\代码\stdafx 依赖\stdafx 依赖\stdafx dependancy.cpp(33):错误 C2146: 语法错误:缺少';'前 标识符“myHelper”
d:\代码\stdafx 依赖\stdafx 依赖\stdafx dependancy.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;
}

【问题讨论】:

    标签: c++ visual-c++ mfc compiler-errors stdafx.h


    【解决方案1】:

    这个链接一定会给你一些线索。 Purpose of stdafx.h

    之前定义的行 #include "stdafx.h" 被编译器忽略。因此,如果您想实际包含这些文件,则需要在 #include "stdafx.h". 之后包含它们

    希望清楚。

    【讨论】:

      【解决方案2】:

      除了 ckv 的回答之外,在“stdafx.h”之前包含头文件几乎没有意义,因为任何重要的头文件都将包含其中包含的框架类型(例如任何 MFC 类型)。

      【讨论】:

        猜你喜欢
        • 2011-07-30
        • 1970-01-01
        • 2015-10-18
        • 2021-11-23
        • 2017-11-07
        • 2011-03-20
        • 1970-01-01
        • 1970-01-01
        • 2021-04-29
        相关资源
        最近更新 更多