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:
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.Code: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 ===|
Here is the main function and the header and source that are giving me the problems
Code: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
Code:#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>&); http://cboard.cprogramming.com/register.php?do=addmember #endif // STUDENT_INFO_H_INCLUDED
My source file Student_info.cpp
Code:#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.
Operating System : Linux (Ubuntu)
Compiler : GNU GCC Compiler
IDE : Code::Blocks



LinkBack URL
About LinkBacks


