LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2006, 10:39 PM   #1
orfiyus
Member
 
Registered: May 2006
Posts: 42

Rep: Reputation: 15
Problems with header files.


Hi

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
 
Old 07-07-2006, 10:41 PM   #2
orfiyus
Member
 
Registered: May 2006
Posts: 42

Original Poster
Rep: Reputation: 15
Here is the error message. The previous post was too long with it. I had to put into a code box cuz the the site was complaining about posting images.

Code:
Error message:

/tmp/cc1qikgM.o: In function `main':dtimedemo.cpp:(.text+0x8a): undefined reference to `DigitalTime::DigitalTime()'
:dtimedemo.cpp:(.text+0x95): undefined reference to `DigitalTime::DigitalTime()'
:dtimedemo.cpp:(.text+0xbc): undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, DigitalTime&)'
:dtimedemo.cpp:(.text+0xdb): undefined reference to `DigitalTime::advance(int)'
:dtimedemo.cpp:(.text+0xed): undefined reference to `operator==(DigitalTime const&, DigitalTime const&)'
:dtimedemo.cpp:(.text+0x12a): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, DigitalTime const&)'
:dtimedemo.cpp:(.text+0x15f): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, DigitalTime const&)'
:dtimedemo.cpp:(.text+0x18a): undefined reference to `DigitalTime::advance(int, int)'
:dtimedemo.cpp:(.text+0x1bf): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, DigitalTime const&)'

END OF ERROR MESSAGE
 
Old 07-07-2006, 10:50 PM   #3
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
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:
Code:
g++ -o dtime.o dtime.cpp
g++ -o dtimedemo dtimedemo.cpp dtime.o
or maybe it can work on a single line:

Code:
g++ -o dtimedemo dtimedemo.cpp dtime.cpp
I hope this is useful
Cheers!
 
Old 07-07-2006, 11:18 PM   #4
orfiyus
Member
 
Registered: May 2006
Posts: 42

Original Poster
Rep: Reputation: 15
yo thanks alot demon. The two liner didnt work but the one all one line did.

Thanks for looking at it.

later.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
KDE 3.5.1 Konstruct has problems finding necessary header files JcN Linux - Newbie 2 02-06-2006 03:57 PM
Including header files and source files for classes Feenix Programming 8 09-28-2005 10:53 AM
gcc header files and cpp files? EchO Linux - Software 3 03-01-2005 01:14 AM
Header files Pyus Linux - Software 0 11-03-2004 09:36 AM
c header files in linux in place of header files in windows? harun_acs Programming 1 03-17-2004 02:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration