我正在使用 Visual Studio 2005。我创建了一个基于 MFC 的控制台应用程序,名为“StdAfx 依赖”。IDE 为我创建了以下文件。
- 资源.h
- StdAfx 依赖.h
- 标准数据文件
- StdAfx 依赖.cpp
- stdafx.cpp
CHelper
我用 Helper.h 和 Helper.cpp添加了另一个类,如下所示。
助手.h:
#pragma once
class CHelper
{
public:
CHelper(void);
~CHelper(void);
};
助手.cpp
#include "StdAfx.h"
#include "Helper.h"
CHelper::CHelper(void)
{
}
CHelper::~CHelper(void)
{
}
CHelper
我在主函数中创建了一个对象;为了实现这一点,我在 StdAfx Dependancy.cpp 的第一行添加了 Header.h 文件,如下所示;我得到了以下错误。
d:\codes\stdafxdependancy\stdafxdependancy\stdafxdependancy.cpp(33):错误 C2065:'CHelper':未声明的标识符
d:\codes\stdafxdependancy\stdafxdependancy\stdafxdependancy.cpp(33):错误 C2146:语法错误:缺少 ';' 在标识符 'myHelper'
d:\codes\stdafxdependancy\stdafxdependancy\stdafxdependancy.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;
}