I tried some codes from Deitel & Deitel's books & whenever I tried codes with headers or classes I will receive a linking error message like this one:
Configuration: fig06_08 - Win32 Debug--------------------
Linking...
fig06_08.obj : error LNK2001: unresolved external symbol "public: void __thiscall Time::printStandard(void)" (?printStandard@Time@@QAEXXZ)
fig06_08.obj : error LNK2001: unresolved external symbol "public: void __thiscall Time::printMilitary(void)" (? printMilitary@Time@@QAEXXZ)
fig06_08.obj : error LNK2001: unresolved external symbol "public: __thiscall Time::Time(int,int,int)" (??0Time@@QAE@HHH@Z)
Debug/fig06_08.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.
fig06_08.exe - 4 error(s), 0 warning(s)
This puzzled me because when I compiled the file with my Visual C++ 6 there was no error or warning. I tried to run the following program with my Borland C++ 5.02 & it wasn't working either.
Can anyone tell me what is wrong? This is very frustrating because I am learning classes with C++.
Thanks.
time2.h source code:
Code:
#ifndef TIME2_H
#define TIME2_H
class Time
{
public:
Time(int = 0, int = 0, int = 0);
void setTime(int, int, int);
void printMilitary();
void printStandard();
private:
int hour;
int minute;
int second;
};
#endif
time2.cpp source code:
Code:
#include<iostream>
using std::cout;
#include "time2.h"
Time::Time(int hr, int min, int sec)
{setTime(hr, min, sec);}
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::printMilitary()
{
cout<<(hour < 10 ? "0" : "") <<hour<< ":"
<<(minute < 10 ? "0" : "") << minute;
}
void Time::printStandard()
{
cout<<((hour == 0 || hour == 12) ? 12 : hour % 12)
<<":" << (minute < 10 ? "0" : "") << minute
<<":" << (second < 10 ? "0" : "") << second
<<( hour < 12 ? "AM" : "PM");
}
main.cpp source code:
Code:
#include<iostream>
using std::cout;
using std::endl;
#include "time2.h"
int main()
{
Time t1,
t2(2),
t3(21,34),
t4(12,25,42),
t5(27,74,99);
cout<<"Constructed with:\n"
<<"all arguments defaulted:\n ";
t1.printMilitary();
cout<<"\n ";
t1.printStandard();
cout<<"\nhour specified; minute and second defaulted:"
<<"\n ";
t2.printMilitary();
cout<<"\n ";
t2.printStandard();
cout<<"\nhour and minute specified: second defaulted:"
<<"\n ";
t3.printMilitary();
cout<<"\n ";
t3.printStandard();
cout<<"\nhour, minute, and second specified:"
<<"\n ";
t4.printMilitary();
cout<<"\n ";
t4.printStandard();
cout<<"\nall invalid values specified:"
<<"\n ";
t5.printMilitary();
cout<<"\n ";
t5.printStandard();
cout<<endl;
return 0;
}