Undefined reference

Why this code is giving me undefined reference to Time
Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef TIME_H
#define TIME_H
// Time class definition
class Time
{
public:
    Time(); // constructor
    void setTime( int, int, int ); // set hour, minute and second
    void printUniversal(); // print time in universal-time format
    void printStandard(); // print time in standard-time format
private:
    int hour; // 0 - 23 (24-hour clock format)
    int minute; // 0 - 59
    int second; // 0 - 59
}; // end class Time
#endif 

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
#include "Time.h"
#include <iostream>
#include <iomanip>
using namespace std;
Time::Time()
{
    hour=minute=second=0;
}
void Time::setTime( int h, int m, int s)
{
    (h>=0 && h<24)? h:0;
    (m>=0 && m<60)? m:0;
    (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" );
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Time.h"
#include <iostream>
using namespace std;
int main()
{
  Time t;
  t.printStandard();
  cout<<endl;
  t.setTime(11,13,34);
  t.printStandard();
  cout<<endl;
  t.printUniversal();
}
Because you're not compiling and linking to time.cpp (or whatever you've named the second set of code in your post)
Thank you ,how shall I do that
I would do it like this:

g++ time.cpp main.cpp

How you do it depends on your compiler/linker and what you've named the files.
@Moschops is not using any IDE for wiriting his programs, so you can use this approach if you also don't use one. That is wirte each file separately (in whatever text editor you like) and compile them together.

On the other hand if you use one then you can check if all files are included in your project. Probably you leave some file out will compiling. You specically need to tell which files are included in your project
Topic archived. No new replies allowed.