0

This very well could be a syntax error on my part since I am rather new with using multiple files and structs in C++ (in particular, passing structs to functions). Here are the three files:

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

When using TextPad/G++, I get the following error:

/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

When using an online C++ compiler (CompileOnline), I get:

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

I wasn't able to find a guide on how to set up TextPad/G++ to compile and link multiple files, but my instructor gave a short set of instructions that I followed. Here is how it's set up:

TextPad/G++ Multiple File Compile

So this could a two-parter question (how do I set up TextPad to correctly compile/link files? why is my honorStatus() function undefined in main.cpp?) or it could just be that my syntax is wrong. I'm honestly not sure. Sorry if this is a bit long; I wanted to include as much detail as possible. Any help is greatly appreciated.

korina
  • 39
  • 3
  • 9
  • you seem to have two files that define a "main" function... since you compile *.cpp; you might be compiling more than you wanted? – Chris Maes Apr 13 '14 at 09:10

3 Answers3

3

The problem is that you are compiling "*.cpp" all together. Given this

/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

we can see that the the compiler has been trying to combine p5.cpp, p7b.cpp and test.cpp into one executable (possibly other .cpp files too).

You need to actually tell the compiler exactly which files you want to build together to one program. E.g.

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

(I would suggest also adding -Wall -Wextra -Werror to the compile line, as that allows the compiler to detect the small mistakes that aren't strictly errors, but where you probably got something wrong)

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Thank you! I had my current project's files stored on the desktop as well as some old test files, and I didn't notice that those were being picked up as well. Just moving my current project files into their own folder worked perfectly. I also added your suggestions to the compile line. Just out of curiosity, could you give me an example of some errors/non-errors they might pick up? – korina Apr 13 '14 at 09:18
  • 1
    One example is `if (x = 1)`, when you (probably) mean `if (x == 1)` - there are literally several hundreds of possible warnings in modern compilers. Just checked the source code for "clang", and it has 765 warnings (in the version I have, which is two weeks old). – Mats Petersson Apr 13 '14 at 09:29
1

From the linker output you can see that main function is found in these files: p7b.cpp, p5.cpp and test.cpp. As there's no main.cpp file listed in the linker output, I guess that current directory is setup to be where p7b.cpp and other files are located.

Try to change Initial Folder to be where your main.cpp file is set (something like /cygdrive/c/Users/Korina/programming/). Also, remove all unrelevant files from that directory, as you're compiling all cpp files.

Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
1

The error message is clear enough. Your project contains the following files

p7b.cpp, p5.cpp, test.cpp

where in each file there is defined function main. Put a place in order with your project files.

As for the error message when you use the inline compiler then it seems module functs.cpp is not included in the project. So the compiler does not see the function definition.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335