0

私は C++ で複数のファイルと構造体を使用することに慣れていないため (特に構造体を関数に渡す)、これは私の構文エラーである可能性があります。以下に 3 つのファイルを示します。

main.cpp:

#include <iostream>
#include <fstream>
#include <string>
#include "common.h"
using namespace std;

void honorStatus(int, student studentList[]);

int main(void)
{
    int header;
    string filename;
    ifstream inputFile;
    student studentList[MAX_STUDENTS];

    // Get filename from user and try to open file
    cout << "Please enter a filename: ";
    cin >> filename;

    inputFile.open(filename.c_str());

    // If file cannot be opened, output error message and close program
    if (inputFile.fail())
    {
        cout << "Input file could not be opened. Please try again." << endl;
        return 1;
    }

    // Get header number from file. If header is larger than max number
    // of students, error is output and program is closed
    inputFile >> header;
    if (header > MAX_STUDENTS)
    {
        cout << "Number of students has exceeded maximum of " << MAX_STUDENTS
             << ". Please try again." << endl;
        return 1;
    }

    // Read file information (student ID, hours, and GPA) into struct array
    for (int i = 0; i < header; i++)
    {
        inputFile >> studentList[i].ID >> studentList[i].hours >> studentList[i].GPA;
    }

    // Close the file
    inputFile.close();

    // Calls function honorStatus
    honorStatus(header, studentList);

    return 0;
}

functs.cpp:

#include <iostream>
#include "common.h"
using namespace std;

// Function to determine classification and honors society eligibility requirements
// of each student, outputting this information and the number of students eligible
void honorStatus(int fheader, student fstudentList[])
{
    int cnt = 0;

    for (int i = 0; i < fheader; i++)
    {
        if (fstudentList[i].hours < 30)
        {
            cout << "Student #" << fstudentList[i].ID << " is a freshman with GPA of "
            << fstudentList[i].GPA << ".    Not eligible." << endl;
        }
        else if (fstudentList[i].hours > 29 && fstudentList[i].hours < 60)
        {
            if (fstudentList[i].GPA >= 3.75)
            {
                cout << "Student #" << fstudentList[i].ID << " is a sophomore with GPA of "
                << fstudentList[i].GPA << ".    Eligible." << endl;
                cnt++;
            }
            else
            {
                cout << "Student #" << fstudentList[i].ID << " is a sophomore with GPA of "
                << fstudentList[i].GPA << ".    Not Eligible." << endl;
            }
        }
        else if (fstudentList[i].hours > 59 && fstudentList[i].hours < 90)
        {
            if (fstudentList[i].GPA >= 3.5)
            {
                cout << "Student #" << fstudentList[i].ID << " is a junior with GPA of "
                << fstudentList[i].GPA << ".    Eligible." << endl;
                cnt++;
            }
            else
            {
                cout << "Student #" << fstudentList[i].ID << " is a junior with GPA of "
                << fstudentList[i].GPA << ".    Not eligible." << endl;
            }
        }
        else
        {
            if (fstudentList[i].GPA >= 3.25)
            {
                cout << "Student #" << fstudentList[i].ID << " is a senior with GPA of "
                << fstudentList[i].GPA << ".    Eligible." << endl;
                cnt++;
            }
            else
            {
                cout << "Student #" << fstudentList[i].ID << " is a senior with GPA of "
                << fstudentList[i].GPA << ".    Not eligible." << endl;
            }
        }
    }

    cout << "\nTotal number of students eligible for the Honor Society is " << cnt << "." << endl;
}

common.h:

// Maximum number of students allowed
const int MAX_STUDENTS = 10;

// Struct for student info
struct student
{
    int ID;
    int hours;
    float GPA;
};

TextPad/G++ を使用すると、次のエラーが発生します。

/cygdrive/c/Users/Korina/AppData/Local/Temp/ccxq9DAh.o:p7b.cpp:(.text+0x0): multiple definition of `main'
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o:p5.cpp:(.text+0x0): first defined here
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccLa96oD.o:test.cpp:(.text+0x0): multiple definition of `main'
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o:p5.cpp:(.text+0x0): first defined here
/usr/lib/gcc/i686-pc-cygwin/4.8.2/../../../../i686-pc-cygwin/bin/ld: /cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o: bad reloc address 0x1b in section `.text$_ZNSt11char_traitsIcE7compareEPKcS2_j[__ZNSt11char_traitsIcE7compareEPKcS2_j]'
collect2: error: ld returned 1 exit status

オンライン C++ コンパイラ (CompileOnline) を使用すると、次のようになります。

/tmp/ccIMwHEt.o: In function `main':
main.cpp:(.text+0x1cf): undefined reference to `honorStatus(int, student*)'
collect2: error: ld returned 1 exit status

複数のファイルをコンパイルおよびリンクするように TextPad/G++ をセットアップする方法に関するガイドを見つけることができませんでしたが、インストラクターが一連の簡単な手順を教えてくれました。設定方法は次のとおりです。

TextPad/G++ 複数ファイルのコンパイル

したがって、これは 2 部構成の質問 (ファイルを正しくコンパイル/リンクするように TextPad を設定するにはどうすればよいですか? honorStatus()main.cpp で関数が定義されていないのはなぜですか?) または単に構文が間違っている可能性があります。正直なところよくわかりません。これが少し長い場合は申し訳ありません。できるだけ詳細を含めたかったのです。どんな助けでも大歓迎です。

4

3 に答える 3

3

問題は、「*.cpp」をまとめてコンパイルしていることです。これを考えると

/cygdrive/c/Users/Korina/AppData/Local/Temp/ccxq9DAh.o:p7b.cpp:(.text+0x0): multiple definition of `main'
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o:p5.cpp:(.text+0x0): first defined here
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccLa96oD.o:test.cpp:(.text+0x0): multiple definition of `main'
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o:p5.cpp:(.text+0x0): first defined here
/usr/lib/gcc/i686-pc-cygwin/4.8.2/../../../../i686-pc-cygwin/bin/ld: /cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o: bad reloc address 0x1b in section `.text$_ZNSt11char_traitsIcE7compareEPKcS2_j[__ZNSt11char_traitsIcE7compareEPKcS2_j]'
collect2: error: ld returned 1 exit status

コンパイラが p5.cpp、p7b.cpp、および test.cpp を 1 つの実行可能ファイル (おそらく他の .cpp ファイルも) に結合しようとしていることがわかります。

どのファイルを 1 つのプログラムにまとめてビルドするかをコンパイラーに正確に伝える必要があります。例えば

g++ main.cpp functs.cpp -o main.exe 

(コンパイル行にも追加-Wall -Wextra -Werrorすることをお勧めします。これにより、厳密にはエラーではなく、おそらく何か問題が発生した小さな間違いをコンパイラが検出できるようになります)

于 2014-04-13T09:11:21.953 に答える
1

リンカーの出力から、main関数が次のファイルにあることがわかります: p7b.cppp5.cppおよびtest.cpp. リンカー出力にファイルがリストされていないため、現在のディレクトリがどこに設定され、他のファイルが配置main.cppされていると思います。p7b.cpp

ファイルが設定されInitial Folderている場所(のようなもの)に変更してみてください。また、すべてのファイルをコンパイルしているため、そのディレクトリから無関係なファイルをすべて削除します。main.cpp/cygdrive/c/Users/Korina/programming/cpp

于 2014-04-13T09:12:04.793 に答える