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!
time1.h
#ifndef TIME1_H
#define TIME1_H
class Time {
public:
Time();
void setTime( int, int, int );
void printUniversal();
void printStandard();
private:
int hour;
int minute;
int second;
};
#endif
time1.cpp
#include <iostream>
using std::cout;
#include <iomanip>
using std::setfill;
using std::setw;
#include "time1.h"
Time::Time()
{
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");
}
time.cpp
#include <iostream>
using std::cout;
using std::cin;
using std:: endl;
#include "time1.h"
int main()
{
Time t;
cout << "The initial universal time is ";
t.printUniversal();
cout << "\nThe initial standard time is ";
t.printStandard();
t.setTime( 13, 27, 6 );
cout << "\n\nUniversal time after setTime is ";
t.printUniversal();
cout << "\nStandard time after setTime is ";
t.printStandard();
t.setTime( 99, 99, 99);
cout << "\n\nAfter attempting invalid settings: "
<< "\nUniversal time: ";
t.printUniversal();
cout << "\nStandard time: ";
t.printStandard();
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
owen@Aineko:~/programming/cplusplus/TimeBookEx$