2010-06-22 82 views
2

我正在使用Visual Studio 2005.我创建了一个名为“StdAfx依赖性”的基于MFC的控制台应用程序。 IDE为我创建了以下文件。StdAfx +头文件 - 包含在MFC应用程序中的顺序

  1. RESOURCE.H
  2. StdAfx Dependancy.h
  3. stdafx.h中
  4. StdAfx Dependancy.cpp
  5. stdafx.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;为了达到这个目的,我在StdAfx Dependancy.cpp的第一行添加了Header.h文件,如下所示;我得到了以下错误。

d:\代码\ stdafx扶养\ stdafx 扶养\ stdafx dependancy.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; 
} 

回答

5

此链接必须给你一些线索。 Purpose of stdafx.h

#include "stdafx.h"限定的线由编译器忽略。所以如果你想实际包含这些文件,那么你需要在#include "stdafx.h".

之后包含它们。希望它很清楚。

+0

你的意思是'#include“stdafx.h”'? – bdhar 2010-06-22 06:19:55

+0

对不起,错了 – ckv 2010-06-22 06:23:23

1

除了ckv的回答,在“stdafx.h”之前包含头文件是没有意义的,因为任何非平凡的头文件都将包含那里包含的框架类型(例如任何MFC类型)。