【发布时间】:2011-03-06 15:37:00
【问题描述】:
我正在使用 Visual Studio 2005。我创建了一个基于 MFC 的控制台应用程序,名为“StdAfx 依赖”。 IDE 为我创建了以下文件。
- 资源.h
- StdAfx Dependancy.h
- stdafx.h
- StdAfx Dependancy.cpp
- stdafx.cpp
我用 Helper.h 和 Helper.cpp 添加了另一个类 CHelper,如下所示。
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:\codes\stdafx 依赖关系\stdafx 依赖\stdafx 依赖.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;
}
【问题讨论】:
标签: c++ visual-c++ mfc compiler-errors stdafx.h