Thank you for all the replies. The issue was precisely those three variables. However, besides removing them from the header and adding them locally to main.cpp, I also had to declare them in clockTypeImp.cpp in order for the program to run because I kept getting an error about the getTime function requesting them, but the debugger told me that the variables had not been declared in that scope.
Even though the program runs now, I have noticed that I am getting what I imagine are memory addresses as the output for object myClock, something that wasn't happening when the whole thing was in a single file. The reason I had those variables declared globally was also because that's how I managed to make the program run before I split it into the three files.
ndrewxie: Hello. The book did mention what you said, and I hoped that CodeBlocks was properly linking the files, which, thankfully, it was. I did have the header included in main though. I should probably learn how to do it without relying on a external source, but I am feeling a bit overwhelmed as it is. I only have so much time.
Here's what I get as an output now:
1 2 3 4 5 6
|
The default myClock time is: 1988324058:-2:-233419117
The default yourClock time is: 00:00:00
The default oneClock time with one parameter declared is: 15:00:00
The default twoClock time with two parameters declared is: 05:25:00
Enter hours, minutes, and seconds in the format (h m s):
|
clock.h without variables:
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
|
#include <iostream>
#ifndef clockType_h
#define clockType_h
class clockType
{
public:
void setTime (int, int, int); //Sets time to a given value.
void getTime (int&, int&, int&) const;
void printTime () const; //Prints time.
void incrementSeconds(); //Increments time by 1 second.
void incrementMinutes(); //Increments time by 1 minute.
void incrementHours(); //Increments time by 1 hour.
bool equalTime (const clockType&) const; //Compares equality
//of two times.
clockType(int = 0, int = 0, int =0);
//Functions that overload the operators.
bool operator==(const clockType& otherClock) const;
bool operator!=(const clockType& otherClock) const;
bool operator<(const clockType& otherClock) const;
bool operator>(const clockType& otherClock) const;
void operator=(const clockType& otherClock);
private:
int hr;
int min;
int sec;
};
#endif
|
Added variables to clockTypeImp.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include<iostream>
#include "clockType.h"
#ifndef clockType_Imp
#define clockType_Imp
//Variables used in setTime and getTime functions. It was necessary
//to declare them in this file and main.cpp as well in order for
//the program to work properly.
int hours;
int minutes;
int seconds;
using namespace std;
.
.
.
|
Variables added to main.cpp:
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
|
#include<iostream>
#include "clockType.h"
using namespace std;
int main()
{
//Variables used in setTime and getTime functions. It was necessary
//to declare them in this file and main.cpp as well in order for
//the program to work properly.
int hours;
int minutes;
int seconds;
clockType oneClock (15);
clockType twoClock (5, 25);
clockType yourClock;
clockType equalClock;
clockType myClock (12, 30, 30);
cout << "The default myClock time is: ";
myClock.getTime(hours, minutes, seconds);
cout << hours << ":" << minutes << ":" << seconds;
cout << endl;
.
.
.
|
If you check the part of the code I posted above, you will see that I do set myClock to receive 12, 30, 30, but now the program is not assigning those values any longer.
Note: I didn't post the whole code again because I can only post a max of 8192 characters here.