Does X code have a problem with inheritance in C++

Discussion in 'Mac Programming' started by guardian85, Feb 29, 2008.

  1. guardian85 macrumors newbie

    Joined:
    Feb 29, 2008
    #1
    Hello im trying to compile my project in Xcode but it gives me 117 compile errors. When i have tried the code in other compilers it has compiled just fine. I create a project by going to New Project->Command Line Utility -> C++

    45 of the errors are with the Time.cpp file
    72 of the errors are with the Date.cpp file


    Appointment.h
    Code:
    #ifndef APPOINTMENT_H_
    #define APPOINTMENT_H_
    #include <iostream>
    #include "Date.h"
    #include "Time.h"
    #include <string>
    using namespace std;
    
    class Appointment : public Date, public Time
    {
    
    public:
    	    Appointment();
    		Appointment(int,int,int,int,int,int,string);//(month,day,year,hour,minute,second,Description
    		~Appointment();
    		string getDescription();
    		void printAppointment();
    		void setDescription(string);
    		friend ostream& operator << (ostream&, Appointment&);
    		friend istream& operator >> (istream&, Appointment&);
    		
    private:
    	string desc;
    };
    
    #endif /*APPOINTMENT_H_*/
    

    Appointment.cpp
    Code:
    #include "Appointment.h"
    #include <iomanip>
    #include <iostream>
    #include <string>
    #include "Time.h"
    #include "Date.h"
    using namespace std;
    
    Appointment::Appointment(int month, int day, int year,int hour,int min,int sec,string des):Date(month,day,year), Time(hour,min,sec){
    desc = des;
    }
    Appointment::Appointment(){
    }
    Appointment::~Appointment(){}
    
    void Appointment :: setDescription(string s){
    	desc = s;
    }
    
    string Appointment:: getDescription(){
    		return desc;
    }
    
    void Appointment :: printAppointment(){
    	Date::printDate();
    	cout << endl;
    	Time::printTime();
    	cout << endl<< desc << endl;
    	
    }
    
    
    ostream& operator << (ostream &out,  Appointment &a){//
    	//cerr << "inside Appointment"<< endl;
    	// Date::operator<<endl;
    	//out << a.Time::printTime() <<endl;
    	out << a.getMonth() << "/" <<a.getDay()<<"/"<<a.getYear() << endl;
    	out << a.getTempHour()<<":" << a.getMinute()<<":" << a.getSecond() << " "<<a.getAmPm()<<endl;
    	out << a.desc << endl;
    	return out;
    	
    }
    
    istream& operator >> (istream &in,  Appointment &a){
    	int num;
    	string app;
    	in >> num;
    	a.setMonth(num);
    	in.ignore(); //ignores    /
    	in >> num;
    	a.setDay(num);
    	in.ignore(); // ignores /
    	in >> num;
    	a.setYear(num);
    	in.ignore(); // ignores    space
    	in>>num;
    	a.setHour(num);
    	in.ignore();//ignores     :
    	in >> num;
    	a.setMinute(num);
    	in.ignore(); //    ignores :
    	in >> num;
    	a.setSecond(num);
    	in.ignore();    ////       ignores space
    	getline(cin,app);
    	a.setDescription(app);
    	
    	return in;
    }
    
    Time.h
    Code:
    #ifndef TIME_H_
    #define TIME_H_
    #include <string>
    #include <iostream>
    using namespace std;
    //a time consists of hour, minute, second
    //and AM/PM
    //military time (24hour)
    //normal/regular (12 hour)
    //hour can have the value 0-23
    //minute can have value 0-59
    //second can have value 0-59
    class Time {
    private:
    	int hour;
    	int tempHour;
    	int minute;
    	int second;
    	string amPm;
    public:
    	friend ostream& operator<<(ostream&, const Time &);
    	friend istream& operator>>(istream&, Time &);
    	void printTime();//normal 12 hour
    	void printMilitary();//24 hour
    	void tick();//increments seconds by 1
    	int getHour();
    	int getMinute();
    	int getSecond();
    	void setHour(int);
    	void setMinute(int);
    	void setSecond(int);
    	Time(int, int, int); //constructor
    	Time();
    	string getAmPm();
    	void setAmPm(string);
    	int getTempHour();
    	void setTempHour(int);
    	//this constructor takes 3 pieces of information
    	
    	virtual ~Time();//deconstructor
    };
    
    #endif /*TIME_H_*/
    
    Time.cpp
    Code:
    #include "Time.h"
    #include <iostream>
    #include <iomanip>
    using namespace std;
    //constructor takes hour (h), minute (m),
    //and second (s) and uses that to initialize
    //the private data members
    Time::Time(int h, int m, int s)
    {
    	//we really should check that h, m, and s
    	//are valid, how would we do that?
    	setHour(h);
    	setMinute(m);
    	setSecond(s);
    }
    
    Time::~Time()
    {
    }
    //tick function/method
    void Time::tick(){
    //need to increment seconds by 1
    	second++;
    	//did I go over 59?
    	if (second > 59) {
    		minute++;
    		second=0;
    		//did minutes go over 59, if so add to hour
    		if (minute > 59) {
    			hour++;
    			minute=0;
    			//did hour go over 23, if so reset to 0
    			if(hour > 23){
    				hour=0;
    			}
    		}
    	}
    }//end tick method
    //printTime method
    void Time::printTime(){
    //prints the time in 12 hour format
    //hh:mm:ss
    //hour doesn't print right, we need a temporary
    //hour variable to print 1PM-11PM correctly
    	
    	//this statement does the same thing as the
    	//following if/else statement
    	//you can look it up
    //	int temphour = (hour>12) ? hour-12 : hour;
    	int temphour;
    	if(hour > 12)
    		temphour = hour-12;//gives us the right
    	//hour number 13 = 1, 14 = 2, etc.
    	else
    		temphour = hour;
    	//the setfill function fills empty space with
    	//the specified character, in this case zero
    	cout << setfill('0') << setw(2) 
    	     << temphour << ":"; 
    	
    	cout << setw(2) << minute << ":"
    	     << setw(2) << second;
    	//how about the AM/PM?
    	if (hour < 12) //0-11
    		cout << " AM";
    	else
    		cout << " PM";
    }
    void Time::printMilitary(){
    //prints the time in 12 hour format
    //hh:mm:ss
    //hour doesn't print right, we need a temporary
    //hour variable to print 1PM-11PM correctly
    	
    	//this statement does the same thing as the
    	//following if/else statement
    	//you can look it up
    	//the setfill function fills empty space with
    	//the specified character, in this case zero
    	cout << setfill('0') << setw(2) 
    	     << hour << ":"; 
    	
    	cout << setw(2) << minute << ":"
    	     << setw(2) << second;
    }
    int Time::getHour() {
    	return hour;
    }
    int Time::getMinute() {
    	return minute;
    }
    int Time::getSecond() {
    	return second;
    }
    void Time::setHour(int h) {
    	hour = (h >= 0 && h<24)? h : 0;
    }
    void Time::setMinute(int m) {
    	minute = (m >=0 && m < 60) ? m : 0;
    }
    void Time::setSecond(int s) {
    	second = (s >= 0 && s < 60) ? s : 0;
    }
    ostream& operator<<(ostream &out, const Time &t) {
    	out << setw(2) << setfill('0');
    	out << t.hour << ":"
    	    << setw(2) << t.minute << ":"
    	    << setw(2) << t.second;
    	return out;
    }//end overloaded stream insertion operator
    istream& operator>>(istream &in, Time &t) {
    	in>>t.hour;
    	in.ignore();//ignore ':'
    	in>>t.minute;
    	in.ignore();//ignore ':'
    	in>>t.second;
    	return in;
    }
    Time::Time(){}
    
    string Time::getAmPm(){
    	if (hour >11){
    		amPm = "PM";
    	}else{
    		amPm = "AM";}
    	return amPm;
    }
    void Time::setAmPm(string timeofday){
    	amPm = timeofday;
    }
    
    int Time ::getTempHour(){
    	tempHour =hour;
    	if(hour > 12){
    		tempHour = hour -12;
    	return tempHour;
    	}else{
    		return tempHour;
    	}
    }
    
    void Time::setTempHour(int h){
    	tempHour = h;
    }
    
    
    Date.h
    Code:
    #ifndef DATE_H
    #define DATE_H
    #include <iostream>
    using std::ostream;
    using std::istream;
    class Date {
    public:
       friend ostream& operator<<(ostream&,const Date&);
       friend istream& operator>>(istream&,Date&);
       Date();
       Date(int m);
       Date(int m, int d);
       Date(int m, int d, int y);
       void printDate();
       void  setMonth(int m); 
       void  setDay(int d); 
       void  setYear(int y); 
    
       int  getMonth(); 
       int  getDay(); 
       int  getYear(); 
       virtual ~Date(); 
    private:
       int month;
       int day;
       int year;
    };//end date class
    #endif
    
    Time.cpp
    Code:
    #include "Time.h"
    #include <iostream>
    #include <iomanip>
    using namespace std;
    //constructor takes hour (h), minute (m),
    //and second (s) and uses that to initialize
    //the private data members
    Time::Time(int h, int m, int s)
    {
    	//we really should check that h, m, and s
    	//are valid, how would we do that?
    	setHour(h);
    	setMinute(m);
    	setSecond(s);
    }
    
    Time::~Time()
    {
    }
    //tick function/method
    void Time::tick(){
    //need to increment seconds by 1
    	second++;
    	//did I go over 59?
    	if (second > 59) {
    		minute++;
    		second=0;
    		//did minutes go over 59, if so add to hour
    		if (minute > 59) {
    			hour++;
    			minute=0;
    			//did hour go over 23, if so reset to 0
    			if(hour > 23){
    				hour=0;
    			}
    		}
    	}
    }//end tick method
    //printTime method
    void Time::printTime(){
    //prints the time in 12 hour format
    //hh:mm:ss
    //hour doesn't print right, we need a temporary
    //hour variable to print 1PM-11PM correctly
    	
    	//this statement does the same thing as the
    	//following if/else statement
    	//you can look it up
    //	int temphour = (hour>12) ? hour-12 : hour;
    	int temphour;
    	if(hour > 12)
    		temphour = hour-12;//gives us the right
    	//hour number 13 = 1, 14 = 2, etc.
    	else
    		temphour = hour;
    	//the setfill function fills empty space with
    	//the specified character, in this case zero
    	cout << setfill('0') << setw(2) 
    	     << temphour << ":"; 
    	
    	cout << setw(2) << minute << ":"
    	     << setw(2) << second;
    	//how about the AM/PM?
    	if (hour < 12) //0-11
    		cout << " AM";
    	else
    		cout << " PM";
    }
    void Time::printMilitary(){
    //prints the time in 12 hour format
    //hh:mm:ss
    //hour doesn't print right, we need a temporary
    //hour variable to print 1PM-11PM correctly
    	
    	//this statement does the same thing as the
    	//following if/else statement
    	//you can look it up
    	//the setfill function fills empty space with
    	//the specified character, in this case zero
    	cout << setfill('0') << setw(2) 
    	     << hour << ":"; 
    	
    	cout << setw(2) << minute << ":"
    	     << setw(2) << second;
    }
    int Time::getHour() {
    	return hour;
    }
    int Time::getMinute() {
    	return minute;
    }
    int Time::getSecond() {
    	return second;
    }
    void Time::setHour(int h) {
    	hour = (h >= 0 && h<24)? h : 0;
    }
    void Time::setMinute(int m) {
    	minute = (m >=0 && m < 60) ? m : 0;
    }
    void Time::setSecond(int s) {
    	second = (s >= 0 && s < 60) ? s : 0;
    }
    ostream& operator<<(ostream &out, const Time &t) {
    	out << setw(2) << setfill('0');
    	out << t.hour << ":"
    	    << setw(2) << t.minute << ":"
    	    << setw(2) << t.second;
    	return out;
    }//end overloaded stream insertion operator
    istream& operator>>(istream &in, Time &t) {
    	in>>t.hour;
    	in.ignore();//ignore ':'
    	in>>t.minute;
    	in.ignore();//ignore ':'
    	in>>t.second;
    	return in;
    }
    Time::Time(){}
    
    string Time::getAmPm(){
    	if (hour >11){
    		amPm = "PM";
    	}else{
    		amPm = "AM";}
    	return amPm;
    }
    void Time::setAmPm(string timeofday){
    	amPm = timeofday;
    }
    
    int Time ::getTempHour(){
    	tempHour =hour;
    	if(hour > 12){
    		tempHour = hour -12;
    	return tempHour;
    	}else{
    		return tempHour;
    	}
    }
    
    void Time::setTempHour(int h){
    	tempHour = h;
    }
    
    Appointmentproject.cpp // This file is the test file

    Code:
    #include "Time.h"
    #include "Date.h"
    #include "Appointment.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(){
    	Appointment test;
    	cin >> test;
    	cout << test;
    	return 0;
    	
    }
    
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  3. guardian85 thread starter macrumors newbie

    Joined:
    Feb 29, 2008
    #3
    /Developer/SDKs/MacOSX10.5.sdk/usr/include/c++/4.0.0/ctime:66: error: '::clock_t' has not been declared



    there is not an error with one of the lines of code more like perhaps the time class may have been previously defined, i say that because when i double click on the error it takes me to ctime. If that is the case why did it compile on Eclipse?
     
  4. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #4
    just a slightly OT remark, but unless this an exercise in multiple inheritance, you really should be using composition in this case instead of MI
     
  5. guardian85 thread starter macrumors newbie

    Joined:
    Feb 29, 2008
    #5
    i wanted to try multiple inheritance just to play with it. I talked to the teacher and he didn't care if i did it that way. I turned the project in so people don't have to worry about plagiarism or any of that kind of stuff. i just want to figure out why i cant get the classes to work with xcode, so ill know for future projects.
     
  6. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #6
    Hi

    I think your problem is that you've named one of your project files Time.h.

    b e n

    EDIT: I should have added that Time.h is clashing with time.h in your project paths, and ctime includes time.h I believe.
     
  7. guardian85 thread starter macrumors newbie

    Joined:
    Feb 29, 2008
    #7
    That was it. It is weird though that the files would compile in Eclipse and not throw those errors. Thank You everyone for your help
     
  8. Krevnik macrumors 68030

    Krevnik

    Joined:
    Sep 8, 2003
    #8
    Case-sensitivity. One likely is, the other likely isn't.

    In general though, you want to avoid similar names that will clash with standard libraries.

    (I hate the C++ std library for this reason... The C std lib uses "string.h" for string functions, and the C++ std lib uses "string" for the string class)
     

Share This Page