please help - compiler error using GNU GCC compiler - unidentified reference to...

This is a discussion on please help - compiler error using GNU GCC compiler - unidentified reference to... within the C++ Programming forums, part of the General Programming Boards category; I'm new to c++ programming and started to work my way through the book Accelerated C++. When I tried to ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    6

    please help - compiler error using GNU GCC compiler - unidentified reference to...

    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:
    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 ===|
    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
    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
    Last edited by goldn; 12-13-2010 at 06:57 PM.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    I don't know Code::Blocks, but are Student_info.cpp and Student_info.h part of the project?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to provide all the files to the compiler, not just one. Since you're using an IDE, that means you need to set up a project, put all the files in the project, and then build the entire project.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    6
    Yes all of the files are in the project, I can see them in my project manager. I think its something to do with the compiler and not the program itself.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have them all in sources/headers as appropriate? What does your build log say?

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    6
    Build log :

    Code:
    -------------- Build: Debug in 4.0 ---------------
    
    Linking console executable: bin/Debug/4
    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&)'
    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 0 seconds)
    3 errors, 0 warnings
    Build messages :
    Code:
    ||=== 4.0, Debug ===|
    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 ===|
    Last edited by goldn; 12-13-2010 at 09:35 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That is extremely interesting, because your build log should start with the command that was given.

    My suggestion would be build-clean, make sure you've got both .cpp files listed in the sources thing on the left, and then build-build.

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    6
    Ok thanks, ill try that and post back with my results

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC - Strange networking functions error.
    By maththeorylvr in forum Windows Programming
    Replies: 3
    Last Post: 04-05-2005, 01:00 AM
  2. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  3. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 12:02 PM
  4. compiler problem
    By sofarsoclose in forum C Programming
    Replies: 3
    Last Post: 07-10-2003, 12:39 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21