Good afternoon,
I've got an assignement where i need to set up a parent class and a child class with .cpp files. When I try to call the a function in the child class i get and undefined reference error. I'm able to initialize this class using the default constructor with the child class.
In main i'm using the following:
TimeChild ctime(1235);//initialize child
and I'm getting the following error:
[Linker error] undefined reference to 'TimeChild:setInt(int)'
any help is appreciated:
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:
int hours;
int mins;
int secs;
string ap;
void adjustTime(TimeClass); //to convert time when added
void subTime(TimeClass); //to convert time when subtracted
public: // Time Class
//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 =
};
#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(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(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 program
#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;
//set up the values for the parent class
TimeClass regtime(12,23,48,"PM"); //set an initial regular time
TimeClass miltime(12,23,48); //set an initial regular time
//set up the values for the child class
TimeChild ctime(1235);//initialize chile
mtime = "0840";
//ctime(1235);
system("PAUSE");
return EXIT_SUCCESS;
}