Calling function from class
Sep 9, 2014 at 4:29pm UTC
I was given this class type and am supposed to get input from the user. My error says "undefined reference to clockType::clockType()"
This is the header file
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 26 27 28 29 30 31 32 33 34 35 36
class clockType
{ public :
void setTime(int hours, int minutes, int seconds);
void getTime(int & hours, int & minutes, int & seconds) const ;
void printTime() const ;
void incrementSeconds();
void incrementMinutes();
void incrementHours();
bool equalTime(const clockType& otherClock) const ;
clockType(int hours, int minutes, int seconds);
clockType();
private :
int hr; //stores the hours
int min; //stores the minutes
int sec; //stores the seconds
};
The cpp file
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
// the source code is from Malik, chapter 1.
//
//clockTypeImp.cpp file
#include <iostream>
#include "clockType.h"
using namespace std;
void clockType::setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;
if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;
if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}
void clockType::getTime(int & hours, int & minutes, int & seconds) const
{
hours = hr;
minutes = min;
seconds = sec;
}
void clockType::printTime() const
{ if (hr < 10)
cout<<"0" ;
cout<<hr<<":" ;
if (min < 10)
cout<<"0" ;
cout<<min<<":" ;
if (sec < 10)
cout<<"0" ;
cout<<sec<<endl<<endl;
}
void clockType::incrementHours()
{
hr++;
if (hr > 23)
hr = 0;
}
void clockType::incrementMinutes()
{
min++;
if (min > 59)
{
min = 0;
incrementHours(); //increment hours
}
}
void clockType::incrementSeconds()
{
sec++;
if (sec > 59)
{
sec = 0;
incrementMinutes(); //increment minutes
}
}
bool clockType::equalTime(const clockType& otherClock) const
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}
clockType::clockType(int hours, int minutes, int seconds)
{
setTime(hours, minutes, seconds);
}
clockType::clockType() //default constructor
{
setTime(0, 0, 0);
}
and this is what I was trying in main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include "clockType.h"
using namespace std;
int main()
{
clockType cellPhoneClock;
clockType myWatch;
int hrs, mins, secs;
cout << "Please enter the hours for the cell phone clock.\n" ;
cellPhoneClock.setTime(hrs, mins, secs);
// write your code here!
return 0;
}
Sep 9, 2014 at 4:33pm UTC
The "undefined reference" means the linker did not find the implementation of your default constructor.
Are you sure clocktype.cpp got compiled and included in the link step?
Sep 9, 2014 at 4:43pm UTC
To be honest, I'm pretty new to using different files this way. Do you think you could explain how to link it?
Sep 9, 2014 at 5:16pm UTC
Are you using an IDE or a makefile?
Sep 9, 2014 at 5:22pm UTC
I'm using an IDE (CodeBlocks), but I'm supposed to use a makefile. I just don't know how to do it :/
Topic archived. No new replies allowed.