Visual Studio 2005 を使用しています。「StdAfx 依存関係」という名前の MFC ベースのコンソール アプリケーションを作成しました。IDE によって次のファイルが作成されました。
- Resource.h
- StdAfx Dependency.h
- stdafx.h
- StdAfx 依存関係.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:\codes\stdafx 依存関係\stdafx 依存関係\stdafx 依存関係.cpp(33): エラー C2065: 'CHelper': 宣言されていない識別子
d:\codes\stdafx 依存関係\stdafx 依存関係\stdafx 依存関係.cpp(33): エラー C2146:構文エラー: ';' がありません 識別子 'myHelper'
d:\codes\stdafx 依存関係\stdafx 依存関係\stdafx 依存関係.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;
}