I thought I had a pretty good handle on this class thing, but once again, the compiler has humbled me.
Here's what I have. There are 3 files. I made sure they're all included in the same project before compiling.
Code:
/***************************************
CTime.h
by Meghan
11/17/05
Lab 10
Contains the definition of the class
CTime.
****************************************/
class CTime
{
public:
CTime(); //default constructor
CTime(int, int, int); //constructor
~CTime(); //destructor
void getHours(int&) const; //function prototypes
void getMinutes(int&) const;
void getSeconds(int&) const;
void setHours(int);
void setMinutes(int);
void setSeconds(int);
CTime add(CTime);
private:
int hours;
int minutes;
int seconds;
};
CTime::CTime() //default constructor
{
hours = 0;
minutes = 0;
seconds = 0;
}
inline CTime::CTime(int hr, int min, int sec) //constructor
{
hr = 0;
min = 0;
sec = 0;
}
inline void CTime::getHours(int& hr) const
{
hr = hours;
}
inline void CTime::getMinutes(int& min) const
{
min = minutes;
}
inline void CTime::getSeconds(int& sec) const
{
sec = seconds;
}
inline void CTime::setHours(int hr)
{
if (hr<=60)
hr = hours;
else
hr = 0;
}
inline void CTime::setMinutes(int min)
{
if (min<=60)
min = minutes;
else
min = 0;
}
inline void CTime::setSeconds(int sec)
{
if (sec<=60)
sec = seconds;
else
sec = 0;
}
Code:
/***************************************
CTime.cpp
by Meghan
11/17/05
Lab 10
Contains the CTime member function that
adds the two times together.
****************************************/
#include <cstdlib>
#include <iostream>
#include "CTime.h"
using namespace std;
CTime CTime::add(CTime t2)
{
long t1Seconds;
long t2Seconds;
long t3Seconds;
CTime temp;
//converts time1 from hours:minutes:seconds to seconds
t1Seconds = (long)hours*3600 + (long)minutes*60 + (long)seconds;
//converts time2 from hours:minutes:seconds to seconds
t2Seconds = (long)t2.hours*3600 + (long)t2.minutes*60 + (long)t2.seconds;
//adds time1 and time2
t3Seconds = t1Seconds + t2Seconds;
temp.hours = t3Seconds/60;
temp.minutes = (t3Seconds%60)/60;
temp.seconds = (t3Seconds%60)%60;
return temp;
}
Code:
/***************************************
TimeApp.cpp
by Meghan
11/17/05
Lab 10
A program that gets two time values
from the user, adds them, and displays
the result.
****************************************/
#include <cstdlib>
#include <iostream>
#include "CTime.h"
using namespace std;
int main()
{
CTime time1; //first time value from user input
CTime time2; //second time value from user input
CTime sumTime; //sum of times the user entered
char ch; //holds the colon character
int hrs1; //hours value of time1
int min1; //minutes value of time1
int sec1; //seconds value of time1
int hrs2; //hours value of time2
int min2; //minutes value of time2
int sec2; //seconds value of time2
int sumHrs; //hours value of sum
int sumMin; //minutes value of sum
int sumSec; //seconds value of sum
cout << "Enter a time in 00:00:00 format:" << endl;
cin >> hrs1;
cin.get(ch); //gets the colon character
cin >> min1;
cin.get(ch);
cin >> sec1;
time1.setHours(hrs1); //stores user input in hours value
time1.setMinutes(min1); //stores user input in minutes value
time1.setSeconds(sec1); //stores user input in seconds value
cout << "Enter another time in the same format:" << endl;
cin >> hrs2;
cin.get(ch); //gets the colon character
cin >> min2;
cin.get(ch);
cin >> sec2;
time2.setHours(hrs2); //stores user input in hours value
time2.setMinutes(min2); //stores user input in minutes value
time2.setSeconds(sec2); //stores user input in seconds value
sumTime = time1.add(time2); //adds the two times together
sumTime.getHours(sumHrs);
sumTime.getMinutes(sumMin);
sumTime.getSeconds(sumSec);
cout << "The sum of " << hrs1 << ":" << min1 << ":";
cout << sec1 << " and " << hrs2 << ":" << min2;
cout << ":" << sec2 << " is " << sumHrs << ":";
cout << sumMin << ":" << sumSec << endl;
return 0;
}
The compiler says (several times apiece):
"multiple definition of `CTime::CTime()' "
" first defined here "
"[Linker error] undefined reference to `CTime::~CTime()' "
See anything I'm doing wrong? Any ideas are appreciated. Thanks.