undefined reference to (errors) something about my compiler linking

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;




As far as I can tell, there is nothing wrong with my code, I double checked it with the book's code a few times. It's something to do with my compiler's method of linking and I don't know how to link or what option I need to change so that it does link.

Compiler : GNU GCC Compiler
IDE : Code::Blocks
You probably forgot to add Student_info.cpp to your project.
If you don't do that, it won't be linked (or even compiled).
It's in my project folder and it opens when I open the project, thanks though.
That's not sufficient. In the project management window to the left it needs to appear under "Sources".
All of my headers and sources are under the header and source trees in the project management window.
Last edited on
Topic archived. No new replies allowed.