Hello All,

I am trying to learn C++ on my own, but I think I am having some problems with Code::Blocks. I am familiar with Java (I did some CS as an undergrad) and I feel that the C++ basics are pretty easy to translate from Java; however, I am having problems with my compiler and it has been quite frustrating. My problem is as follows: I am using the Accelerated C++ book to learn since I am already familiar with OOP. I did a program from one of their chapters that uses header files (it's a gradebook code). The compiler keeps giving me "undefined reference to" errors (e.g. "undefined reference to 'read(std::istream&, Student_info&)' and the exact compiler error is listed at the end of this post) when I compile. I define the function read in the *.c file and I've done everything like the book says (looking for any misspelled word, etc). I can include the code if needed, but I thought I would see if it was something simple that I had forgotten (the code is a little lengthy). Also, all the files are in the same directory. Any help would be much appreciated. I attached the code to this message. Another problem, and I don't know if this is related, when I try to compile the *.cpp files one by one, I usually get a "C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\libmingw32.a(main.o):main.c:(.text+0x104)||undefined reference to `_WinMain@16'|" error. Thanks so much!


C:\Documents and Settings\Allen\My Documents\C++ Work\Grade_Project\gradeCalc.o:gradeCalc.cpp:(.text+0x210)||undefined reference to `read(std::istream&, Student_info&)'|
C:\Documents and Settings\Allen\My Documents\C++ Work\Grade_Project\gradeCalc.o:gradeCalc.cpp:(.text+0x28a)||undefined reference to `compare(Student_info const&, Student_info const&)'|
C:\Documents and Settings\Allen\My Documents\C++ Work\Grade_Project\gradeCalc.o:gradeCalc.cpp:(.text+0x414)||undefined reference to `grade(Student_info const&)'|
||=== Build finished: 3 errors, 0 warnings ===|

#include <stdexcept>
#include <vector>
#include "grade.h"
#include "median.h"
#include "Student_info.h"

using std::domain_error;
using std::vector;

double grade(double midterm, double final, double homework)
    return 0.2*midterm + 0.4*final + 0.4*homework;

double grade(double midterm, double final, const vector<double>& hw)
    if (hw.size() == 0)
        throw domain_error("student has no homework scores");
    return grade(midterm, final, median(hw));

double grade(const Student_info& s)
    return grade(s.midterm, s.final, s.homework);

#include <vector>
#include "Student_info.h"

double grade(double, double, double);
double grade(double, double, const std::vector<double>&);
double grade(const Student_info&);

#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "grade.h"
#include "Student_info.h"

using std::cin;
using std::setprecision;
using std::cout;
using std::sort;
using std::domain_error;
using std::streamsize;
using std::endl;
using std::string;
using std::max;
using std::vector;

int main()
    Student_info record;
    string::size_type maxlen = 0;       // the length of the longest name

    // read and store all the students' data
    // Invariant: vs contains all the students records
    //            max contains the longest name in vs
    while (read(cin,record))
        // find length of longest name
        maxlen = max(maxlen, record.name.size());

    // alphabetize the student records
    sort(students.begin(), students.end(), compare);

    // write the names and grades
    for (vector<Student_info>::size_type i = 0;
            i != students.size(); ++i)

        // write the name, padded on the right to maxlen + 1 characters
        cout << students[i].name
        << string(maxlen + 1 - students[i].name.size(), ' ');

        // compute and write the grade
            double final_grade = grade(students[i]);
            streamsize prec = cout.precision();
            cout << setprecision(3) << final_grade
            << setprecision(prec);
        catch (domain_error e)
            cout << e.what();
        cout << endl;
    return 0;
#include <algorithm>        // to get the declaration of sort
#include <stdexcept>        // to get the declaration of domain_error
#include <vector>           // to get the declaration of vector

using std::domain_error;    using std::sort;    using std::vector;

// compute the median of a vector<double>
double median(vector<double> vec)
    typedef vector<double>::size_type vec_sz;

    vec_sz size = vec.size();
    if (size == 0)
        throw domain_error("median of an empty vector");


    vec_sz mid = size/2;

    return size % 2 == 0 ? (vec[mid]+vec[mid-1])/2 : vec[mid];

double median(std::vector<double>);

#include "Student_info.h"

using std::istream;
using std::vector;

bool compare(const Student_info& x, const Student_info& y)
    return x.name < y.name;

istream& read(istream& is, Student_info& s)
    // read and store the student's name, midterm grade, and final exam grade
    is >> s.name >> s.midterm >> s.final;

    read_hw(is, s.homework);  // read and store all the student's homework grades
    return is;

istream& read_hw(istream& in, vector<double>& hw)
    if (in)
        // get rid of previous contents

        // read homework grades
        double x;
        while (in >> x)

        // clear the stream so that input will work for the next student
    return in;

#include <iostream>
#include <string>
#include <vector>

struct Student_info {
    std::string name;
    double midterm, final;
    std::vector<double> homework;

bool compare(const Student_info&, const Student_info&);
std::istream& read(std::istream&, Student_info&);
std::istream& read_hw(std::istream&, std::vector<double>&);

7 Years
Discussion Span
Last Post by fatalbert55
Featured Replies
  • 1
    Agni 370   7 Years Ago

    If you are compiling them one-by-one make sure you only compile and don't go to the linking process. I think you might be doing that and while trying to create an executable its not finding main. Read the compiler man pages and find the flag used to only compile and … Read More


Dear fatalbert55,

Give me a reason why should I read the attached files. We are here to help somebody; not to provide a complete solution.

Please post the phrase of code where you got problems.


If you are compiling them one-by-one make sure you only compile and don't go to the linking process. I think you might be doing that and while trying to create an executable its not finding main. Read the compiler man pages and find the flag used to only compile and not link. Once everything is compiled you can link the object files together and create the executable.

Votes + Comments
Seems about right to me.

Thanks Agni! That did the trick!

adatapost: I didn't include snippets of code because it wasn't a part of the code that was causing the problem. I was having problems with the compiler.

Thanks all for helping out the newb!


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.