我正在使用Visual Studio 2005.我創建了一個名爲「StdAfx依賴性」的基於MFC的控制檯應用程序。 IDE爲我創建了以下文件。StdAfx +頭文件 - 包含在MFC應用程序中的順序
- RESOURCE.H
- StdAfx Dependancy.h
- stdafx.h中
- StdAfx Dependancy.cpp
- 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;
}
你的意思是'#include「stdafx.h」'? – bdhar 2010-06-22 06:19:55
對不起,錯了 – ckv 2010-06-22 06:23:23