Good afternoon,
I'm working on a class assignmeent trying to use overloaded math operators + - but i'm getting the following error when I compile.
Line 29 of the main program generates this error:
Linker error undefined reference to 'TimeClass::operator+(TimeClass const&)
any help is appreciated.
Here is the code:
TimeClass.h
#ifndef TIMECLASS_H // Multiple Use Protection
#define TIMECLASS_H
#include <iostream> // Needed for cout and cin
#include <string> // Needed for string objects
using namespace std;
//TimeClass
class TimeClass
{
private:
void adjustTime(TimeClass); //to convert time when added
void subTime(TimeClass); //to convert time when subtracted
public: // Time Class
int hours;
int mins;
int secs;
string ap;
//default constructor
TimeClass()
{hours = mins = secs = 0;ap = "0";}
//function prototype convert string to integer
int NumberSet(string);
//Mutator function for regular time
void setTime(int h,int m,int s, string am_pm)
{hours = h;mins=m;secs=s;ap = am_pm;}
//Mutator function for military time
void setTime(int h,int m,int s)
{hours = h;mins=m;secs=s;}
//constructor for XX:XX:XX AM/PM
TimeClass(int h,int m,int s,string am_pm)
{hours = h;mins=m;secs=s;ap = am_pm;}
//constructore for HH:MM:SS
TimeClass(int h,int m,int s)
{hours = h;mins=m;secs=s;}
//overloaded operators
TimeClass operator + (const TimeClass &); //overloaded +
TimeClass operator - (const TimeClass &); //overloaded -
// TimeClass operator = (const TimeClass &); //overloaded =
};
//Non-member functions prototype(per Professor Shulman's example)
ostream& operator << (ostream &retOut, TimeClass const ®time)
{
retOut << regtime.hours<<":"<<regtime.mins<<":"<<regtime.secs<<":"<<regtime.ap<<endl ;
return retOut;
}
istream& operator >> (istream ®In, TimeClass ®time)
{
//Prompt for hours
cout << "Enter hours: ";
regIn >> regtime.hours;
cout << "Enter minutes: ";
regIn >> regtime.mins;
cout << "Enter seconds: ";
regIn >> regtime.secs;
return regIn;
}
#endif
TimeClass.cpp
#include <ctime> // Used to get todays info
#include <cctype> // Used for is..., to...
using namespace std;
#include "TimeClass.h"
//convert time for addition
void TimeClass::adjustTime(TimeClass)
{
//if the user enters a huge number this will keep looping until secs, mins, hours are reduced
while(secs >= 60)
{
secs -= 60;
mins += 1;
}
while(mins >= 60)
{
mins -= 60;
hours += 1;
}
while(hours > 12)
//switches back and forth each time 12 hours are incremented
{
hours = hours % 12;
if (ap =="AM" )
{
ap = "PM";
}
else if(ap=="PM")
{
ap = "AM";
}
}
}
//convert time for subtraction
void TimeClass::subTime(TimeClass)
{
}
// *** Converts Number in String to Integer (Skips leading blanks) ***
int NumberSet(string asVal)
{ int iDx;
int iNum = 0;
for (iDx = 0; iDx < asVal.length(); iDx++)
if (asVal[iDx] != ' ')
break;
for (iDx = 0; iDx < asVal.length(); iDx++)
{ if (!isdigit(asVal[iDx]))
break;
iNum = iNum * 10 + (asVal[iDx] - '0');
}
return iNum;
}
//overloaded + operator
TimeClass TimeClass::operator + (const TimeClass &right)
{
TimeClass newtime;
newtime.hours = hours + right.hours;
newtime.mins = mins + right.mins;
newtime.secs = secs + right.secs;
adjustTime(newtime);
return newtime;
}
//overloaded - operator
TimeClass TimeClass::operator - (const TimeClass &right)
{
TimeClass newtime;
newtime.hours = hours - right.hours;
newtime.mins = mins - right.mins;
newtime.secs = secs - right.secs;
adjustTime(newtime);
return newtime;
}
//overloaded = operator
//TimeClass TimeClass::operator = (const TimeClass &right)
//{
// TimeClass newtime;
//
// newtime.hours = hours = right.hours;
// newtime.mins = mins = right.mins;
// newtime.secs = secs = right.secs;
// //subtime(newtime);
// return newtime;
// }
//
TimeChild.h
#ifndef TIMECHILD_H
#define TIMECHILD_H
#include <cstring>
#include <string>
#include <cstdlib> //necessary for the atoi conversion function
#include "TimeClass.h"
using namespace std;
class TimeChild : public TimeClass // Inherited from TimeClass
{
private:
string military;
string tstring_1;
string tstring_2;
string holdstring;
int t1_int;
int t2_int;
int test;
public: // Time Class
void setInt(int);
//default constructor
TimeChild()
{military = "0000";}
//constructor
TimeChild(string m)
{setTime(m);}
TimeChild(int v)
{test = v;
setInt(v);}
void setTime(string); //defined in tTmeChild.cpp
};
#endif
TimeChild.cpp
#include <ctime>
#include <cctype>
#include "TimeChild.h"
using namespace std;
void TimeChild::setTime(string miltime)
{
tstring_1 = miltime.substr(0,1);
t1_int = NumberSet(tstring_1);
tstring_2 = miltime.substr(2,3);
t2_int = NumberSet(tstring_2);
}
void TimeChild::setInt(int v)
{
int q;
q = v;
}
// *** Converts Number in String to Integer (Skips leading blanks) ***
int NumberSet(string asVal)
{ int iDx;
int iNum = 0;
for (iDx = 0; iDx < asVal.length(); iDx++)
if (asVal[iDx] != ' ')
break;
for (iDx = 0; iDx < asVal.length(); iDx++)
{ if (!isdigit(asVal[iDx]))
break;
iNum = iNum * 10 + (asVal[iDx] - '0');
}
return iNum;
}
Main
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include "TimeChild.h"
//#include "TimeClass.h"
using namespace std;
int main(int argc, char *argv[])
{
string mtime;
TimeClass first, second;
//set up the values for the parent class
TimeClass regtime(12,23,48,"PM"); //set an initial regular time
TimeClass miltime(1,23,48); //set an initial regular time
miltime.setTime(12,48,56,"AM");
first.setTime(2,3,45);
//pass to overloaded ostream
cout << miltime;
cout << regtime;
//pass to overloaded istream
//enter hours to add
//enter minutes to add
//enter seconds to add
//enter time to add
second = miltime + first;
cout << second;
cin >> first;
cout << first;
//set up the values for the child class
mtime = "0840";
//TimeChild ctime(mtime);
system("PAUSE");
return EXIT_SUCCESS;
}