2010-06-22 89 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類型)。