undefined reference error

I'm somewhat new to programming and am continually having this undefined reference error to all of my functions... can someone tell me what I'm doing wrong? Code is below (sorry for some of the weird documentation, my prof is extremely specific about his requirements for our programs).

/tmp/ccbpfCwv.o: In function `main':
runStudType.cxx:(.text+0xe5): undefined reference to `StudType::StudType()'
runStudType.cxx:(.text+0x2b2): undefined reference to `StudType::ReadData(std::basic_ifstream<char, std::char_traits<char> >&, StudType&)'
runStudType.cxx:(.text+0x2c1): undefined reference to `StudType::FindAvg(StudType&)'
runStudType.cxx:(.text+0x2e9): undefined reference to `StudType::PrintData(std::basic_ofstream<char, std::char_traits<char> >&, StudType)'
runStudType.cxx:(.text+0x2fd): undefined reference to `StudType::ReadData(std::basic_ifstream<char, std::char_traits<char> >&, StudType&)'
collect2: ld returned 1 exit status


//stud.h

#ifndef STUDTYPE_H
#define STUDTYPE_H

#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

class StudType
{
public:
StudType();
StudType(int studIdVal, float exam1Val, float exam2Val, float exam3Val);
void ReadData(ifstream& inFile, StudType& stud);
void PrintData(ofstream& outFile, StudType stud);
void FindAvg(StudType& stud);

private:
int studId;
float exam1;
float exam2;
float exam3;
float avg;
};

#endif


//StudType.cxx

#include "StudType.h"

void StudType::StudType()
//*******************************************************************
// Purpose: This constructor initializes the input data
// Input: none
// Pre: class is declared ok
// Output: none
// Post: Data is given initial value
// Note: none
//*******************************************************************
{
studId = 000;
exam1 = 0;
exam2 = 0;
exam3 = 0;
avg = 0;
}

void StudType::StudType(int studIdVal, float exam1Val, float exam2Val, float exam3Val)
//*************************************************************************************
// Purpose: This constructor initializes the input data
// Input: studIdVal, exam1Val, exam2Val, exam3Val
// Pre: data has value
// Output: none
// Post: Data is given initial value
// Note: none
//**************************************************************************************
{
studId = studIdVal;
exam1 = exam1Val;
exam2 = exam2Val;
exam3 = exam3Val;
}

void StudType::ReadData(ifstream& inFile, StudType& stud)
//*****************************************************************************
// Purpose: This function reads data from inFile and stores it in the object
// Input: inFile
// Pre: inFile is open and ok
// Output: stud
// Post: Data is stored in stud from inFile
// Note: none
//*****************************************************************************
{
inFile >> studId >> exam1 >> exam2 >> exam3;
}

void StudType::FindAvg(StudType& stud)
//*******************************************************************
// Purpose: This function finds the average of three exam scores
// Input: stud
// Pre: stud is declared and has value
// Output: stud
// Post: Average is stored in stud
// Note: none
//*******************************************************************
{
avg = (exam1 + exam2 + exam3) / 3;
}

void StudType::PrintData(ofstream& outFile, StudType stud)
//*******************************************************************
// Purpose: This function prints the data from stud to the outFile
// Input: stud
// Pre: outFile is open and ok, stud has value
// Output: outFile
// Post: Data is printed to the outFile
// Note: none
//*******************************************************************
{
outFile << studId << setw(5) << exam1 << setw(10) << exam2 << setw(10)
<< exam3 << setw(10 << avg << endl;
}


//runStudType.cxx

#include "StudType.h"

int main()
{
ifstream inFile;
ofstream outFile;

inFile.open("in.data");
outFile.open("out.data");

outFile.setf(ios::left);
outFile.precision(2);

StudType stud;

int studCount = 0;

outFile << "*~< Student Exam Report >~*" << endl;
outFile << "Id" << setw(8) << "Exam1" << setw(10) << "Exam2" << setw(10)
<< "Exam3" << setw(10) << "AVG" << endl;
outFile << "--" << setw(8) << "-----" << setw(10) << "-----" << setw(10)
<< "-----" << setw(10) << "---" << endl;

stud.ReadData(inFile, stud);

while(inFile)
{
stud.FindAvg(stud);

stud.PrintData(outFile, stud);

stud.ReadData(inFile, stud);

studCount++;
}

outFile << "Number of Students: " << studCount << endl << endl;
outFile << "*< end>*" << endl;

return 0;
}
Last edited on
Without more information on the error itself, there's very little we can do to help. The error log might be cryptic, but it contains vital information to finding the problem with your code. Rather than summarizing it like you did in your title, I suggest you take a good long look at it and try to decypher it. It's the only way you'll learn to read them!

A quick look at your code makes me believe that the problem resides in StudType.cpp (or .cxx? Weird extension, but okay.): constructors don't have a return type! The "void" return type makes the compiler believe that there's a function StudType::StudType(), which obviously doesn't exist!
Added in the error message, sorry i forgot it originally. But I tried removing the voids from the implementation file and got the same error unfortunately.
Copy-pasted your code into a new project for easier checking.

Removing void from constructors (2x!) in StudType.cpp allowed me to compile everything without problems...

Proof: out.data:
*~< Student Exam Report >~*
IdExam1   Exam2     Exam3     AVG       
-------   -----     -----     ---       
Number of Students: 0

*< end>*

Last edited on
I'll do that next time, sorry it's my first time posting here :) But that's so strange, I'm still getting the error! Any thoughts on what my problem could be? I tried changing the extensions to cpp, I'm not sure why my instructor wanted cxx. Still no luck...
Is it still the exact same errors as you posted, or did anything change?

[edit]

Also, which IDE are you using?
Last edited on
It's the exact same as what was posted. And like I said before, I'm new to programming and not sure what programs are most used. My school uses PuTTY, not sure if you've heard of it. I really appreciate all the help though!
Very confusing...

Here's the code exactly like I have it running on my IDE. Try copy-pasting it to a new project. If that doesn't work on your IDE, there's something wrong there, I think.

(By the way, make sure the filenames match the #include statement.)

http://ideone.com/FpKKo
Topic archived. No new replies allowed.