ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am new to C++ and I am having a problem running a program. The program is 3 files (a header, a file with all the functions defined, and a file with a main() ). I am using g++ and I am compiling on the command line in a terminal. My problem is when I compile the file with the main() function I get an error message, but when I cut and pasted the main() function into the file with all the other function defintions I actually got my desired output for the program. Can someone tell me how to get the file with a stand alone main() function to run the entire program?
I have supplied the error message and the source for the program.
Thanks.
Code:
//Header file dtime.h
#include <iostream>
using namespace std;
class DigitalTime
{
public:
friend bool operator ==(const DigitalTime& time1, const DigitalTime& time2);
DigitalTime(int the_hour, int the_minute);
DigitalTime();
void advance(int minutes_added);
void advance(int hours_added, int minutes_added);
friend istream& operator >>(istream& ins, DigitalTime& the_object);
friend ostream& operator <<(ostream& outs, const DigitalTime& the_object);
private:
int hour;
int minute;
};
//End of dtime.h
dtime.cpp
Code:
#include <iostream>
#include <cctype>
#include <cstdlib>
#include "dtime.h"
using namespace std;
//These function declarations are for use in the defintion of the overloaded
//input operator >>:
void read_hour(istream& ins, int& the_hour);
//Precondition: Next input in the stream ins is a time in 24 hour notation.
//Postcondition: the_hour has been set to the hour part of the time.
//THe colon has been discarded and the next input to be read is the minute.
void read_minute(istream& ins, int& the_minute);
//reads the minute from the stream ins after read_hour has read the hour.
int digit_to_int(char c);
//Precondition: c is one of the digits '0' thru '9'.
//returns the integer for the digit.
//friend function.
bool operator ==(const DigitalTime& time1, const DigitalTime& time2)
{
return (time1.hour == time2.hour && time1.minute == time2.minute);
}
DigitalTime::DigitalTime(int the_hour, int the_minute)
{
if (the_hour < 0 || the_hour >23 || the_minute < 0 || the_minute >59)
{
cout << "Illegal argument to DigitalTime constructor.";
exit(1);
}
else
{
//hour and minute are private global variables.
hour = the_hour;
minute = the_minute;
}
}
DigitalTime::DigitalTime() : hour(0), minute(0)
{
//Body intentionally empty
}
void DigitalTime::advance(int minutes_added)
{
int gross_minutes = minute + minutes_added;
minute = gross_minutes%60;
int hour_adjustment = gross_minutes/60;
hour = (hour + hour_adjustment)%24;
}
void DigitalTime::advance(int hours_added, int minutes_added)
{
hour = (hour + hours_added)%24;
advance(minutes_added);
}
ostream& operator <<(ostream& outs, const DigitalTime& the_object)
{
outs << the_object.hour << ':';
if (the_object.minute < 10)
outs << '0';
outs << the_object.minute;
return outs;
}
istream& operator >>(istream& ins, DigitalTime& the_object)
{
read_hour(ins, the_object.hour);
read_minute(ins, the_object.minute);
return ins;
}
int digit_to_int(char c)
{
return (static_cast<int>(c) - static_cast<int>('0'));
}
void read_minute(istream& ins, int& the_minute)
{
char c1, c2;
ins >> c1 >> c2;
if (!(isdigit(c1) && isdigit(c2)))
{
cout << "Error illegal input to read_minute\n";
exit(1);
}
the_minute = digit_to_int(c1)*10 + digit_to_int(c2);
if (the_minute < 0 || the_minute > 59)
{
cout << "Error illegal input to read_minute\n";
exit(1);
}
}
void read_hour(istream& ins, int& the_hour)
{
char c1, c2;
ins >> c1 >> c2;
if ( !(isdigit(c1) && (isdigit(c2) || c2 == ':')))
{
cout << "Error illegal input to read_hour\n";
exit(1);
}
else
{
the_hour = digit_to_int(c1)*10 + digit_to_int(c2);
ins >> c2;
if (c2 != ':')
{
cout << "Error illegal input to read_hour\n";
exit(1);
}
}
if (the_hour < 0 || the_hour > 23)
{
cout << "Error illegal input to read_hour\n";
exit(1);
}
}
//end of dtime.cpp
Code:
dtimedemo.cpp
#include <iostream>
#include "dtime.h"
using namespace std;
int main()
{
DigitalTime clock, old_clock;
cout << "Enter the time in 24-hour notation: ";
cin >> clock;
old_clock = clock;
clock.advance(15);
if (clock == old_clock)
cout << "Something is wrong.";
cout << "You entered " << old_clock << endl;
cout << "15 minutes later the time will be "
<< clock << endl;
clock.advance(2, 15);
cout << "2 hours and 15 minutes after that \n"
<< "the time will be " << clock << endl;
return 0;
}
//end of dtimedemo.cpp
Hi,
its been a while since I wrote a line of C++ but I believe that this kind of error happens when you are trying dtimedemo.cpp without the object file dtime.o, so the compiler doesnt have the reference to the actual methods to execute.
Your compilation sequence could be:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.