我正在使用 Visual Studio 2005。我创建了一个基于 MFC 的控制台应用程序,名为“StdAfx dependency”。 IDE 为我创建了以下文件。
- 资源.h
- StdAfx 依赖项.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\stdafx 依赖项\stdafx
dependency\stdafx dependency.cpp(33) :
错误 C2065:“CHelper”:未声明
标识符
d:\代码\stdafx
依赖项\stdafx 依赖项\stdafx
dependency.cpp(33):错误C2146:
语法错误:缺少 ';'前
标识符“myHelper”
d:\代码\stdafx
依赖项\stdafx 依赖项\stdafx
dependency.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;
}