0

这很可能是我的语法错误,因为我对在 C++ 中使用多个文件和结构(特别是,将结构传递给函数)相当陌生。以下是三个文件:

主.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;
}

函数.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;
}

常见的.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++ 多文件编译

所以这可能是一个两部分的问题(我如何设置 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 组合成一个可执行文件(也可能是其他 .cpp 文件)。

您需要准确地告诉编译器您想将哪些文件一起构建到一个程序中。例如

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

(我建议也添加-Wall -Wextra -Werror到编译行,因为这允许编译器检测不是严格错误的小错误,但你可能出错了)

于 2014-04-13T09:11:21.953 回答
1

错误信息很清楚。您的项目包含以下文件

p7b.cpp, p5.cpp, test.cpp

在每个文件中都有定义的函数main。将您的项目文件按顺序放置。

至于使用内联编译器时的错误消息,似乎模块functs.cpp 不包含在项目中。所以编译器看不到函数定义。

于 2014-04-13T09:17:16.037 回答
1

从链接器输出中,您可以看到main在以下文件中找到该函数p7b.cppp5.cpptest.cpp. 由于链接器输出中没有main.cpp列出文件,我猜当前目录设置为p7b.cpp其他文件所在的位置。

尝试更改Initial Foldermain.cpp设置文件的位置(类似于/cygdrive/c/Users/Korina/programming/)。此外,在编译所有文件时,从该目录中删除所有不相关的cpp文件。

于 2014-04-13T09:12:04.793 回答