bdh*_*har 2 c++ mfc compiler-errors stdafx.h visual-c++
我正在使用Visual Studio 2005.我创建了一个名为"StdAfx dependancy"的基于MFC的控制台应用程序.IDE为我创建了以下文件.
我CHelper在Helper.h和Helper.cpp中添加了另一个类,如下所示.
Helper.h:
#pragma once
class CHelper
{
public:
CHelper(void);
~CHelper(void);
};
Run Code Online (Sandbox Code Playgroud)
Helper.cpp
#include "StdAfx.h"
#include "Helper.h"
CHelper::CHelper(void)
{
}
CHelper::~CHelper(void)
{
}
Run Code Online (Sandbox Code Playgroud)
我CHelper在main函数中创建了一个对象; 为实现这一点,我在StdAfx Dependancy.cpp的第一行添加了Header.h文件,如下所示; 我有以下错误.
d:\ codes\stdafx dependancy\stdafx dependancy\stdafx dependancy.cpp(33):error C2065:'CHelper':未声明的标识符
d:\ codes\stdafx dependancy\stdafx dependancy\stdafx dependancy.cpp(33):error C2146:语法错误:缺少';' 在标识符'myHelper'之前
d:\ codes\stdafx dependancy\stdafx dependancy\stdafx dependancy.cpp(33):error 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;
}
Run Code Online (Sandbox Code Playgroud)
这个链接必须给你一些线索. stdafx.h的目的
#include "stdafx.h"编译器忽略在此之前定义的行
.因此,如果您想要实际包含这些文件,那么您需要在之后包含它们#include "stdafx.h".
希望很清楚.