Hello DaniWeb!

My first post to you. I have a question about an undefined reference error I am getting. Currently I am studying from Dietel and Dietel C++ How to Program 4th ed, and on page 421 of chapter 6 I am working on the program they have in the book.

It is three files, and I'm pretty sure I typed them correctly.
Here is the header file and the member function file, followed by the main. When I compile this I get the errors show at the very bottom.

Any ideas why? Thank you for the help!


#ifndef TIME1_H
#define TIME1_H

class Time {

        void setTime( int, int, int );
        void printUniversal();
        void printStandard();

        int hour;
        int minute;
        int second;




#include <iostream>

using std::cout;

#include <iomanip>

using std::setfill;
using std::setw;

#include "time1.h"

    hour = minute = second = 0;

void Time::setTime( int h, int m, int s )
    hour = ( h >= 0 && h < 24 ) ? h : 0;
    minute = ( m >= 0 && m < 60 ) ? m : 0;
    second = ( s >= 0 && s < 60 ) ? s : 0;

void Time::printUniversal()
    cout << setfill( '0' ) << setw( 2 ) << hour << ":"
        << setw( 2 ) << minute << ":"
        << setw( 2 ) << second;

void Time::printStandard()
    cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
        << ":" << setfill( '0' ) << setw( 2 ) << minute
        << ":" << setw( 2 ) << second
        << ( hour < 12 ? " AM" : " PM");


#include <iostream>

using std::cout;
using std::cin;
using std:: endl;

#include "time1.h"

int main()
    Time t;

    cout << "The initial universal time is ";
    cout << "\nThe initial standard time is ";

    t.setTime( 13, 27, 6 );

    cout << "\n\nUniversal time after setTime is ";
    cout << "\nStandard time after setTime is ";

    t.setTime( 99, 99, 99);

    cout << "\n\nAfter attempting invalid settings: "
        << "\nUniversal time: ";
    cout << "\nStandard time: ";

    cout << endl;

    return 0;


Error on command line.

owen@Aineko:~/programming/cplusplus/TimeBookEx$ g++ time.cpp -o time
/tmp/cc3hSuo9.o: In function `main':
time.cpp:(.text+0x74): undefined reference to `Time::Time()'
time.cpp:(.text+0x93): undefined reference to `Time::printUniversal()'
time.cpp:(.text+0xb2): undefined reference to `Time::printStandard()'
time.cpp:(.text+0xd5): undefined reference to `Time::setTime(int, int, int)'
time.cpp:(.text+0xf4): undefined reference to `Time::printUniversal()'
time.cpp:(.text+0x113): undefined reference to `Time::printStandard()'
time.cpp:(.text+0x136): undefined reference to `Time::setTime(int, int, int)'
time.cpp:(.text+0x165): undefined reference to `Time::printUniversal()'
time.cpp:(.text+0x184): undefined reference to `Time::printStandard()'
collect2: ld returned 1 exit status

i would make sure that time1.h is in a directory where the compiler looks for the files to include. the compiler might not be finding the include file. I've had this problem with msvc 6.0 but I've never used g++

your problem is in how you are compiling it. You need to compile each .cpp file separately into objects and then link them all together at the end. Give this a try (my g++ flags are a bit rusty, but I think they're right)

g++ -c time1.cpp -o time1.o //compile into an object
g++ -c time.cpp -o time.o //compile into an object
g++ time1.o time.o -o time //link objects together

Hope that helps


Awesome. That worked. Thank you much. Can I ask why I have to compile them into objects first?

owen@Aineko:~/programming/cplusplus/TimeBookEx$ g++ -c time1.cpp -o time1.o;g++ -c time.cpp -o time.o;g++ time1.o time.o -o time
owen@Aineko:~/programming/cplusplus/TimeBookEx$ ./time
The initial universal time is 00:00:00
The initial standard time is 12:00:00 AM

Universal time after setTime is 13:27:06
Standard time after setTime is 1:27:06 PM

After attempting invalid settings: 
Universal time: 00:00:00
Standard time: 12:00:00 AM

Well, I'll try my best. I'm not that keen on compiler workings, but I would be happy to share how I view the process.

in your time.cpp file, you include time1.h. Ignore that you ever wrote time1.cpp for now. if you were to compile it as
g++ time.cpp -o time
naturally the compiler sees the header (time1.h) and looks in time.cpp to give the implementations of those functions. When it doesn't see it, it gives you the error you saw, essentially saying "you said those would be here, but they're not". It gave you that because you only included the "declaration" of the functions.

when you compile the separate .cpp's into objects, for instance:
g++ -c time1.cpp -o time1.o
this makes an object which has BOTH the declaration and implementation of the functions. So when you include "time1.h" in "time.cpp" and compile them as objects, you are essentially telling the compiler how that code relates the the program as a whole. (a puzzle piece and how it fits in the puzzle) When you link the objects together
g++ time1.o time.o -o time
that's when the compiler takes all the individual puzzle pieces together and makes a pretty picture ;)

Hopefully that made sense, I'm sure I made some technical or notation error in there somewhere, but that's how I view it. If you are REALLY curious, I'm sure google can yield some pretty good explanations.

This question has already been answered. Start a new discussion instead.