0

C++ を学習しているチュートリアルのコードを含む Visual Studio 2013 Preview でプロジェクトを作成しています。プロジェクトのメイン ファイルには、同じプロジェクトの別の .cpp ファイルで定義された関数を起動できるプログラムが含まれています。そのコードは以下のとおりです。

#include "stdafx.h"
#include <iostream>
#include "Comments.cpp"
#include "Structure of a Program.cpp"

int structure(void);
void comment(void);

int main()
{
    using namespace std;
    cout << "This project contains files related to Chapter 1 of Learn CPP
        Tutorials\n" << endl;
    cout << "Please type in the tutorial number to view its output:\n" << 
        "1 - Structure of a Program\n" <<
        "2 - Comments\n" <<
         "3 - A First Look at Variables (and cin)\n" <<
        "4 - A First Look at Functions\n" <<
        "5 - A First Look at Operators\n" <<
        "6 - Whitespace & Basic Formatting\n" <<
        "7 - Forward Declarations\n" <<
                "8 - Programs With Multiple Files\n" <<
        "9 - Header Files\n" <<
        "10 - A First Look at the Preprocessor\n" <<
        "11 - How to Design your First Programs\n" << endl;
    int x;
    cin >> x;
    if (x == 1)
    {
        structure();
    }
    if (x == 2)
    {
         comment();
    }
    cout << "Thank you for using this program. Good bye." << endl;
    return 0;
}

私が抱えている問題は、プログラムをビルドするときに、起動している関数が定義されていないにもかかわらず、既に定義されているというエラーが常に発生することです。基本的に、別の .cpp ファイルにあるが同じプロジェクトにある関数を起動する方法について助けが必要です。

ありがとう

4

1 に答える 1

0

.cpp ファイルを含めないでください。

通常、.cpp ファイルをインクルードすると、コードが再コンパイルされますが、そのコードは何度もコンパイルされるべきではないか、コンパイルされない可能性があります (そのため、リンク エラーが発生します)。一方、.h ファイルでは、複数回コンパイルされる可能性のある宣言やその他のものを入れます。それらは、その .h ファイルのインクルードごとに約 1 回コンパイルされます。

この場合、structure() と comment() を宣言したので、おそらく .cpp インクルードを何かで置き換える必要はありません。

于 2013-08-28T12:55:58.497 に答える