Coming from the Java/Eclipse world, just wanted to brush up on C++ and using Code::Blocks.
Was making a basic class and am having trouble implementing it to my main file.
Getting "undefined reference to 'Time::Time(int, int, int)'" and undefined reference errors to all of my methods used in my main.cpp file.
Tried rebuilding, tried re-adding the files to the console project. Not sure what the problem is. I'm fairly rusty with Code::Blocks so maybe it's something obvious, any help would be appreciated.
main.cpp
#include "Time.h"
#include <iostream>
using namespace std;
int main()
{
//Time current();
Time current(12,0,0);
cout << "Current time." << endl;
current.displayTime();
cout << "\nSet time to 22:29:30." << endl;
current.setTime(22,29,30);
current.displayTime();
cout << "\nAdd 45 minutes." << endl;
current.addMinutes(45);
current.displayTime();
cout << "\nAdd 45 seconds." << endl;
current.addSeconds(45);
current.displayTime();
cout << "\nAdd 1 hour." << endl;
current.addHours(1);
current.displayTime();
return 0;
}
Time.h
#ifndef TIME_H
#define TIME_H
class Time
{
public:
Time(int h, int m, int s);
Time(int h, int m);
Time(int h);
Time();
virtual ~Time();
void setTime(int h, int m, int s);
void setTime(int h, int m);
void setTime(int h);
void addHours(int h);
void addMinutes(int m);
void addSeconds(int s);
void displayTime();
int getHours();
void setHours(int h);
int getMinutes();
void setMinutes(int m);
int getSeconds();
void setSeconds(int s);
protected:
private:
int hours;
int minutes;
int seconds;
};
#endif // TIME_H
Time.cpp
#include "Time.h"
#include <iostream>
using namespace std;
Time::Time(int h, int m, int s)
{
hours = h;
minutes = m;
seconds = s;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
seconds = 0;
}
Time::Time(int h)
{
hours = h;
minutes = 0;
seconds = 0;
}
Time::Time()
{
hours = 0;
minutes = 0;
seconds = 0;
}
Time::~Time()
{
//dtor
}
Time::void setTime(int h, int m, int s)
{
hours = h;
minutes = m;
seconds = s;
}
Time::void setTime(int h, int m)
{
hours = h;
minutes = m;
seconds = 0;
}
Time::void setTime(int h)
{
hours = h;
minutes = 0;
seconds = 0;
}
Time::void addHours(int h)
{
hours += h;
if (hours > 23)
{
hours -= 24;
}
}
Time::void addMinutes(int m)
{
minutes += m;
if (minutes > 59)
{
addHours(minutes / 60);
minutes %= 60;
}
}
Time::void addSeconds(int s)
{
seconds += s;
if (seconds > 59)
{
addMinutes(seconds / 60);
seconds %= 60;
}
}
Time::void displayTime()
{
if (hours < 10)
{
cout << "0" << hours;
}
else
{
cout << hours;
}
cout << ":";
if (minutes < 10)
{
cout << "0" << minutes;
}
else
{
cout << minutes;
}
cout << ":";
if (seconds < 10)
{
cout << "0" << seconds;
}
else
{
cout << seconds;
}
cout << endl;
}
Time::void setHours(int h)
{
if (h >= 0 && h <= 23)
{
hours = h;
}
else
{
cout << "Invalid input!\n";
}
}
Time::int getHours()
{
return hours;
}
Time::void setMinutes(int m)
{
if (m >= 0 && m <= 59)
{
minutes = m;
}
else
{
cout << "Invalid input!\n";
}
}
Time::int getMinutes()
{
return minutes;
}
Time::void setSeconds(int s)
{
if (s >= 0 && s <= 59)
{
seconds = s;
}
else
{
cout << "Invald input!\n";
}
}
Time::int getSeconds()
{
return seconds;
}
The class name goes before the method name and not before the return type.
So instead of e.g.
it should be
Time.cpp does not compile because of errors and then the linker can't find any of the symbols.