2

Visual Studio 2005 を使用しています。「StdAfx 依存関係」という名前の MFC ベースのコンソール アプリケーションを作成しました。IDE によって次のファイルが作成されました。

  1. Resource.h
  2. StdAfx Dependency.h
  3. stdafx.h
  4. StdAfx 依存関係.cpp
  5. 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;
}
4

2 に答える 2

5

このリンクは、いくつかの手がかりを提供する必要があります。 stdafx.h の目的

の前に定義された行 #include "stdafx.h"は、コンパイラによって無視されます。したがって、これらのファイルを実際にインクルードしたい場合は、#include "stdafx.h".

それが明確であることを願っています。

于 2010-06-22T06:08:35.120 に答える
1

ckvの回答に加えて、「stdafx.h」の前にヘッダーファイルを含めることはほとんど意味がありません。重要なヘッダーファイルには、そこに含まれるフレームワークタイプ(MFCタイプなど)が含まれるためです。

于 2010-06-22T06:12:32.883 に答える