compiler error: undefined reference to etc...

I'm new to c++ programming and started to work my way through the book Accelerated C++. When I tried to compile the program from Ch4 which had header files and source files I got these errors:
1
2
3
4
5
obj/Debug/main.o||In function `main':|
/home/x/Desktop/programming/4.0/main.cpp|26|undefined reference to `read(std::basic_istream<char, std::char_traits<char> >&, Student_info&)'|
/home/x/Desktop/programming/4.0/main.cpp|33|undefined reference to `compare(Student_info const&, Student_info const&)'|
/home/x/Desktop/programming/4.0/main.cpp|45|undefined reference to `grade(Student_info const&)'|
||=== Build finished: 3 errors, 0 warnings ===|


I tried to find a solution on my own but after hours of searching the only thing I learned is that its something about linking and my GNU GCC compiler. I'm using the IDE Code::Blocks.

Here is the main function and the header and source that are giving me the problems
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
int main()
{
    vector<Student_info> students;
    Student_info record;
    string::size_type maxlen = 0;       // the length of the longest name

    // read and store all the students data.
    // Invariant:  students contains all the student records read so far
    //   maxlen contains the length of the longest name in students
    while (read(cin, record)) {
        // find length of longest name
        maxlen = max(maxlen, record.name.size());
        students.push_back(record);
    }

    // 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
        try {
            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;
}


My header file Student_info.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef STUDENT_INFO_H_INCLUDED
#define STUDENT_INFO_H_INCLUDED

// Student_info.h header file
#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>&);


#endif // STUDENT_INFO_H_INCLUDED 



My source file Student_info.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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 and midterm and final exam grades
    is >> s.name >> s.midterm >> s.final;

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

// read homework grades from an input stream into a `vector'
istream& read_hw(istream& in, vector<double>& hw)
{
    if (in) {
        // get rid of previous contents
        hw.clear();

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

        // clear the stream so that input will work for the next student
        in.clear();
    }
    return in;

Last edited on
Please use the code format tag to format your code. You can simly edit your post an apply the tag around code.

In the file that has main(), did you forget to include Student_info.h?
Edited, yes I did include Student_info.h
When I searched to find a solution, I kept on finding things talking about me having to manually link with the gcc compiler or add some library. I don't know how to manually link and was wondering if I could download something that would do this for me or add a library to do so.
Topic archived. No new replies allowed.