Link to home
Create AccountLog in
Avatar of pacman32689
pacman32689

asked on

Placing user generated char* statement into array of char*s

I believe I am finally making some headway on this cheesy appointment book I am writing. :) However, I have run into some trouble. I have it set so the user can enter up to 10 purposes via giving each appointment day an array of 10 char*s. However, when I go to copy the user input into the the array at a certain position g++ is throwing me the error:

main.cpp:31: error: no matching function for call to 'DailyAppointmentScheduler::setPurpose(const char [12], int)'
DailyAppointmentScheduler.h:29: note: candidates are: void DailyAppointmentScheduler::setPurpose(char**, int)

My understanding is that I am screwing up by trying to find the size of a 2d array item by trying to find the size of it as a 1d array. The two arguments for setPurpose are the actual purpose and then the spot in the array to place it. (I am planning to go back and simply this stuff a lot more, ie combining all the time setting into a single function, but first I have to get the fundamentals right.)

Is there another one of your wonderful examples to set me straight? :) They always help a lot! Thank you.

I also realize my overloaded operators are commented out. My plan was that the << operator would do something alone the lines of cout << Sunday;

And I would get

My daily appointment scheduler for Sunday
10:00 am Go to dentist.
6:30 pm Go to bed.

By making << friends with Date and Time I assume that would be possible. Thanks again!
//main.cpp
 
#include <iostream>
#include "Date.h"
#include "Time.h"
#include "DailyAppointmentScheduler.h"
 
using namespace std;
 
int main ()
{
        person = new char[10];
        strcpy(person,"Paul");
        Date march2(3,2,2008);
        Date march3(3,3,2008);
        Date march4(3,4,2008);
        Date march5(3,5,2008);
        Date march6(3,6,2008);
        Date march7(3,7,2008);
        Date march8(3,8,2008);
        Time su[10];
        Time m[10];
        Time tu[10];
        Time w[10];
        Time th[10];
        Time f[10];
        Time sa[10];
        DailyAppointmentScheduler Sunday(march2,su[10]);
        DailyAppointmentScheduler Monday(march3,m[10]);
        DailyAppointmentScheduler Tuesday(march4,tu[10]);
        DailyAppointmentScheduler Wednesday(march5,w[10]);
        DailyAppointmentScheduler Thursday(march6,th[10]);
        DailyAppointmentScheduler Friday(march7,f[10]);
        DailyAppointmentScheduler Saturday(march8,sa[10]);
        Sunday.setPurpose("Go to jail.",0);
        su[0].setMinutes(30);
        su[0].setHours(12);
        su[0].setTimeOfDay(true);
        Sunday.setPurpose("Get out of jail.",1);
        Sunday.setMinutes(45);
        su[1].setHours(6);
        su[1].setTimeOfDay(true);
        Monday.setPurpose("Brush Teeth", 9);
        m[9].setMinutes(30);
        m[9].setHours(7);
        m[9].setTimeOfDay(false);
        Monday.setPurpose("Go to sleep",3);
        m[3].setMinutes(45);
        m[3].setHours(8);
        m[3].setTimeOfDay(true);
        Tuesday.setPurpose("Go to class.",4);
        tu[4].setMinutes(50);
        tu[4].setHours(2);
        tu[4].setTimeOfDay(true);
        Wednesday.setPurpose("Read book.", 6);
        w[6].setMinutes(27);
        w[6].setHours(5);
w[6].setTimeOfDay(true);
        Thursday.setPurpose("Take test.", 7);
        th[7].setMinutes(22);
        th[7].setHours(9);
        th[7].setTimeOfDay(false);
        Friday.setPurpose("Go to the club.",0);
        f[0].setMinutes(0);  
        f[0].setHours(7);  
        f[0].setTimeOfDay(true); 
        Saturday.setPurpose("Try to start program early.",0);
        sa[0].setMinutes(10); 
        sa[0].setHours(2);
        sa[0].setTimeOfDay(true);
        
}
        
//Time.h
 
#include <iostream>
#ifndef TIME_H
#define TIME_H
class Time
{
        private:
                        int minutes;
                        int hours;
                        bool timeOfDay;
        public:
                Time();
                Time(int,int,bool);
                int getMinutes();
                int getHours();
                void setMinutes(int);
                void setHours(int);
                void setTimeOfDay(bool);
                bool getTimeOfDay();
};
#endif
 
Time.cpp
 
#include "Time.h"
 
Time::Time()
{
        hours = 0;
        minutes = 0;
        setTimeOfDay = false;
}
Time::Time(int hours1, int minutes, bool timeOfDay1)
{
        hours = hours1;
        minutes = minutes;
        timeOfDay = timeOfDay1;
}
int Time::getMinutes()
{
        return minutes;
}
 
void Time::setMinutes(int newMinutes)
{
        minutes = newMinutes;
}
 
int Time::getHours()
{
        return hours;
}
void Time::setHours(int newHours)
{
        hours = newHours;
}
bool Time::getTimeOfDay()
{
        return timeOfDay;
}
void Time::setTimeOfDay(bool newTimeOfDay)
{
        timeOfDay = newTimeOfDay;
}
 
 
//date.h
 
#ifndef DATE_H
#define DATE_H
#include <iostream>
 
class Date
{
        private:
                        int month;
                        int day;
                        int year;
        public:
                        Date();
                        Date(int,int,int);
                        int getMonth();
                        void setMonth(int);
                        int getDay();
                        void setDay(int);
                        int getYear();
                        void setYear(int);
};
#endif
 
//Date.cpp
 
#include "Date.h"
#include <iostream>
 
Date::Date()
{
        day = 0;
        month = 0;
        year = 0;
}
Date::Date(int day1, int month1, int year1)
{
        day = day1;
        month = month1;
        year = year1;
}
int Date::getMonth()
{
        return month;
}
 
void Date::setMonth(int newMonth)
{
        month = newMonth;
}
 
int Date::getDay()
{
        return day;
}
 
void Date::setDay(int newDay)
{
        day = newDay;
}
 
int Date::getYear()
{
        return year;
}
 
void Date::setYear(int newYear)
{
        year = newYear;
}
 
//DailyAppointmentScheduler.h
 
#include <iostream>
#include "Date.h"
#include "Time.h"
#ifndef DAILYAPPOINTMENTSCHEDULER_H
#define DAILYAPPOINTMENTSCHEDULER_H
 
class DailyAppointmentScheduler
{
        private:
                        static char* person;
                        char* purpose[10];
                        Date calendarDate;
                        Time calendarTime[10];
        public:
                        DailyAppointmentScheduler();
                        DailyAppointmentScheduler(Date,Time);
                        const char* getPerson() const;
                        void setPerson(char*);
                        const char* getPurpose() const;
                        void setPurpose(char**,int);
                        ~DailyAppointmentScheduler();
                        DailyAppointmentScheduler &operator=(const DailyAppointmentScheduler&);
                        friend std::ostream& operator<<(std::ostream &os, const Date &d);
                        friend std::ostream& operator<<(std::ostream &os, const Time &t);
};
#endif
 
//DailyAppointmentScheduler.cpp
 
#include "DailyAppointmentScheduler.h"
#include "Time.h"
#include "Date.h"
 
#include <iostream>
 
using namespace std;
 
char * DailyAppointmentScheduler::person = NULL;
 
DailyAppointmentScheduler::DailyAppointmentScheduler()
{
}
DailyAppointmentScheduler::DailyAppointmentScheduler(Date,Time)
{
}
const char* DailyAppointmentScheduler::getPerson() const
{
        return person;
}
void DailyAppointmentScheduler::setPerson(char* newPerson)
{
        delete [] person;
        person = new char[strlen(newPerson+1)];
        strcpy(person,newPerson);
}
const char* DailyAppointmentScheduler::getPurpose() const
{
        return **purpose;
}
void DailyAppointmentScheduler::setPurpose(char** newPurpose, int spot)
{
        purpose[spot] = new char[strlen(newPurpose)+1];
        strcpy(purpose,newPurpose);
        delete [] newPurpose;
}
DailyAppointmentScheduler::~DailyAppointmentScheduler()
{
        delete [] purpose;
        delete person;
}
DailyAppointmentScheduler& DailyAppointmentScheduler::operator=(const DailyAppointmentScheduler& p)
{
    return *this;
}
ostream& operator << (ostream& os, const DailyAppointmentScheduler& s)
{
//      os << s.getPerson() << endl;
//      os << s.getDay() << " " << s.getMonth() << " " s.getYear() <<endl;
//      os << s.getHours() << ":" << s.getMinutes() << " ";
//      if(s.getAmOrPm() == false)
//      {
//              os << "am" << endl;
//      }
//      else
//      {
//              os <<"pm" << endl;
//      }
//      os << s.getPurpose() << endl;
}

Open in new window

Avatar of Infinity08
Infinity08
Flag of Belgium image

You defined the setPurpose method like this :

                        void setPurpose(char**,int);

ie. it takes a pointer to pointer to char.

But :

>> The two arguments for setPurpose are the actual purpose and then the spot in the array to place it.

so, you want a char* as argument ... Just change the char** to char* and try again.
>> so, you want a char* as argument ...
I've been trying to build this for about 10 mins now :( You have a right old mis-mash of **, array and * pointers going on here that might take a while to unravel!

Can I ask you a Q? Is there any reason why donn't just avoid using these (sometimes confusing) constructors for simpler STL containers?
>> My plan was that the << operator would do something alone the lines of cout << Sunday;
>> 
>> And I would get
>> 
>> My daily appointment scheduler for Sunday
>> 10:00 am Go to dentist.
>> 6:30 pm Go to bed.

That looks perfectly valid to me :) And entirely possible.

Do consider providing an operator<< for the Time and Date classes (one for each). Not only will it make your life easier, it will also modularize your code (keep all code to do with the Time class grouped, etc.), which allows to re-use your class, as well as improves the maintainability of your code.
Ok, this now builds.

1. All changes have been commented
2. I moved main from top to bottom so it would build
3. I commented out all your included headers as all the code is inline.
4. I've not tried to modify the code (or review it) beyond making it build

I hope this helps.

>> By making << friends with Date and Time I assume that would be possible
Yes, although you often don't need to make streaming operators friends if you've designed your interface well and all internal data can be accessed via public accessors. Don't make it a friend unless you need to, but don't be afraid to do so if you do need to (friends are just an extension of the public interface).

-Rx.

//Time.h
 
#include <iostream>
#ifndef TIME_H
#define TIME_H
class Time
{
private:
	int minutes;
	int hours;
	bool timeOfDay;
public:
	Time();
	Time(int,int,bool);
	int getMinutes();
	int getHours();
	void setMinutes(int);
	void setHours(int);
	void setTimeOfDay(bool);
	bool getTimeOfDay();
};
#endif
 
//Time.cpp
 
//#include "Time.h"
 
Time::Time()
{
	hours = 0;
	minutes = 0;
	//setTimeOfDay false;
	setTimeOfDay(false);
}
Time::Time(int hours1, int minutes, bool timeOfDay1)
{
	hours = hours1;
	minutes = minutes;
	timeOfDay = timeOfDay1;
}
int Time::getMinutes()
{
	return minutes;
}
 
void Time::setMinutes(int newMinutes)
{
	minutes = newMinutes;
}
 
int Time::getHours()
{
	return hours;
}
void Time::setHours(int newHours)
{
	hours = newHours;
}
bool Time::getTimeOfDay()
{
	return timeOfDay;
}
void Time::setTimeOfDay(bool newTimeOfDay)
{
	timeOfDay = newTimeOfDay;
}
 
 
//date.h
 
#ifndef DATE_H
#define DATE_H
#include <iostream>
 
class Date
{
private:
	int month;
	int day;
	int year;
public:
	Date();
	Date(int,int,int);
	int getMonth();
	void setMonth(int);
	int getDay();
	void setDay(int);
	int getYear();
	void setYear(int);
};
#endif
 
//Date.cpp
 
//#include "Date.h"
#include <iostream>
 
Date::Date()
{
	day = 0;
	month = 0;
	year = 0;
}
Date::Date(int day1, int month1, int year1)
{
	day = day1;
	month = month1;
	year = year1;
}
int Date::getMonth()
{
	return month;
}
 
void Date::setMonth(int newMonth)
{
	month = newMonth;
}
 
int Date::getDay()
{
	return day;
}
 
void Date::setDay(int newDay)
{
	day = newDay;
}
 
int Date::getYear()
{
	return year;
}
 
void Date::setYear(int newYear)
{
	year = newYear;
}
 
//DailyAppointmentScheduler.h
 
#include <iostream>
//#include "Date.h"
//#include "Time.h"
#ifndef DAILYAPPOINTMENTSCHEDULER_H
#define DAILYAPPOINTMENTSCHEDULER_H
 
class DailyAppointmentScheduler
{
private:
	static char* person;
	char* purpose[10];
	Date calendarDate;
	Time calendarTime[10];
public:
	DailyAppointmentScheduler();
	DailyAppointmentScheduler(Date,Time);
	const char* getPerson() const;
	void setPerson(char*);
	const char* getPurpose() const;
	//void setPurpose(char**,int);
	void setPurpose(char*,int);
	~DailyAppointmentScheduler();
	DailyAppointmentScheduler &operator=(const DailyAppointmentScheduler&);
	friend std::ostream& operator<<(std::ostream &os, const Date &d);
	friend std::ostream& operator<<(std::ostream &os, const Time &t);
};
#endif
 
//DailyAppointmentScheduler.cpp
 
//#include "DailyAppointmentScheduler.h"
//#include "Time.h"
//#include "Date.h"
 
#include <iostream>
 
using namespace std;
 
char * DailyAppointmentScheduler::person = NULL;
 
DailyAppointmentScheduler::DailyAppointmentScheduler()
{
}
DailyAppointmentScheduler::DailyAppointmentScheduler(Date,Time)
{
}
const char* DailyAppointmentScheduler::getPerson() const
{
	return person;
}
void DailyAppointmentScheduler::setPerson(char* newPerson)
{
	delete [] person;
	person = new char[strlen(newPerson+1)];
	strcpy(person,newPerson);
}
const char* DailyAppointmentScheduler::getPurpose() const
{
	//        return **purpose;
	return *purpose;
}
//void DailyAppointmentScheduler::setPurpose(char** newPurpose, int spot)
void DailyAppointmentScheduler::setPurpose(char* newPurpose, int spot)
{
	purpose[spot] = new char[strlen(newPurpose)+1];
	//strcpy(purpose,newPurpose);
	strcpy(purpose[spot],newPurpose);
	delete [] newPurpose;
}
DailyAppointmentScheduler::~DailyAppointmentScheduler()
{
	//        delete [] purpose; <--- RX: You can't delete this as it's stack based storage
	delete person;
}
DailyAppointmentScheduler& DailyAppointmentScheduler::operator=(const DailyAppointmentScheduler& p)
{
	return *this;
}
ostream& operator << (ostream& os, const DailyAppointmentScheduler& s)
{
	//      os << s.getPerson() << endl;
	//      os << s.getDay() << " " << s.getMonth() << " " s.getYear() <<endl;
	//      os << s.getHours() << ":" << s.getMinutes() << " ";
	//      if(s.getAmOrPm() == false)
	//      {
	//              os << "am" << endl;
	//      }
	//      else
	//      {
	//              os <<"pm" << endl;
	//      }
	//      os << s.getPurpose() << endl;
 
	// You should return the stream
	return os;
}
 
//main.cpp
 
#include <iostream>
//#include "Date.h"
//#include "Time.h"
//#include "DailyAppointmentScheduler.h"
 
using namespace std;
 
int main ()
{
	//person = new char[10];
	char * person = new char[10];
 
	strcpy(person,"Paul");
	Date march2(3,2,2008);
	Date march3(3,3,2008);
	Date march4(3,4,2008);
	Date march5(3,5,2008);
	Date march6(3,6,2008);
	Date march7(3,7,2008);
	Date march8(3,8,2008);
	Time su[10];
	Time m[10];
	Time tu[10];
	Time w[10];
	Time th[10];
	Time f[10];
	Time sa[10];
	DailyAppointmentScheduler Sunday(march2,su[10]);
	DailyAppointmentScheduler Monday(march3,m[10]);
	DailyAppointmentScheduler Tuesday(march4,tu[10]);
	DailyAppointmentScheduler Wednesday(march5,w[10]);
	DailyAppointmentScheduler Thursday(march6,th[10]);
	DailyAppointmentScheduler Friday(march7,f[10]);
	DailyAppointmentScheduler Saturday(march8,sa[10]);
	Sunday.setPurpose("Go to jail.",0);
	su[0].setMinutes(30);
	su[0].setHours(12);
	su[0].setTimeOfDay(true);
	Sunday.setPurpose("Get out of jail.",1);
 
	// Undefined member
	// Sunday.setMinutes(45);
	su[1].setHours(6);
	su[1].setTimeOfDay(true);
	Monday.setPurpose("Brush Teeth", 9);
	m[9].setMinutes(30);
	m[9].setHours(7);
	m[9].setTimeOfDay(false);
	Monday.setPurpose("Go to sleep",3);
	m[3].setMinutes(45);
	m[3].setHours(8);
	m[3].setTimeOfDay(true);
	Tuesday.setPurpose("Go to class.",4);
	tu[4].setMinutes(50);
	tu[4].setHours(2);
	tu[4].setTimeOfDay(true);
	Wednesday.setPurpose("Read book.", 6);
	w[6].setMinutes(27);
	w[6].setHours(5);
	w[6].setTimeOfDay(true);
	Thursday.setPurpose("Take test.", 7);
	th[7].setMinutes(22);
	th[7].setHours(9);
	th[7].setTimeOfDay(false);
	Friday.setPurpose("Go to the club.",0);
	f[0].setMinutes(0);  
	f[0].setHours(7);  
	f[0].setTimeOfDay(true); 
	Saturday.setPurpose("Try to start program early.",0);
	sa[0].setMinutes(10); 
	sa[0].setHours(2);
	sa[0].setTimeOfDay(true);
 
	// You need this!
	delete [] person;
 
}

Open in new window

Avatar of pacman32689
pacman32689

ASKER

DailyAppointmentScheduler.cpp: In member function 'const char* DailyAppointmentScheduler::getPurpose() const':
DailyAppointmentScheduler.cpp:38: error: invalid conversion from 'char' to 'const char*'
DailyAppointmentScheduler.cpp: In member function 'void DailyAppointmentScheduler::setPurpose(char*, int)':
DailyAppointmentScheduler.cpp:43: error: cannot convert 'char**' to 'char*' for argument '1' to 'char* strcpy(char*, const char*)'
main.cpp: In function 'int main()':
main.cpp:10: error: 'person' was not declared in this scope
main.cpp:33: warning: deprecated conversion from string constant to 'char*'
main.cpp:37: warning: deprecated conversion from string constant to 'char*'
main.cpp:38: error: 'class DailyAppointmentScheduler' has no member named 'setMinutes'
main.cpp:41: warning: deprecated conversion from string constant to 'char*'
main.cpp:45: warning: deprecated conversion from string constant to 'char*'
main.cpp:49: warning: deprecated conversion from string constant to 'char*'
main.cpp:53: warning: deprecated conversion from string constant to 'char*'
main.cpp:57: warning: deprecated conversion from string constant to 'char*'
main.cpp:61: warning: deprecated conversion from string constant to 'char*'
main.cpp:65: warning: deprecated conversion from string constant to 'char*'

1st might be because I am returning ** instead of *
Ok evil, thanks!
Example of a streaming operator -- it's not friends with date it just uses the public interface.

The errors above, are they from the code I provided you? If so, doo as I tested building it on VC2005 and gcc4.x.

>> main.cpp:10: error: 'person' was not declared in this scope
I defiantly fixed this :)

int main ()
{
      //person = new char[10];
      char * person = new char[10];
      ...
}
#include <string>
#include <iostream>
 
class date
{
public:
	date() : date_("04/03/2008"), time_("12:32:19"){}
	std::string const & get_date(){ return date_; }
	std::string const & get_time(){ return time_; }
 
private:
	std::string date_;
	std::string time_;
};
 
// There is no need for the following to be a friend of date, all it needs is exposed in date' public interface
std::ostream & operator << (std::ostream & os, date & d)
{
	os
		<< d.get_date()
		<< " "
		<< d.get_time();
 
	return os;
}
 
int main()
{
	date d;
	std::cout << d << std::endl;
 
	return 0;
}

Open in new window

s/doo/odd :)
You beat me to my list of remaining errors with your corrected code. :)
I am getting this still

main.cpp:33: warning: deprecated conversion from string constant to 'char*'
main.cpp:37: warning: deprecated conversion from string constant to 'char*'
main.cpp:41: warning: deprecated conversion from string constant to 'char*'
main.cpp:45: warning: deprecated conversion from string constant to 'char*'
main.cpp:49: warning: deprecated conversion from string constant to 'char*'
main.cpp:53: warning: deprecated conversion from string constant to 'char*'
main.cpp:57: warning: deprecated conversion from string constant to 'char*'
main.cpp:61: warning: deprecated conversion from string constant to 'char*'
main.cpp:65: warning: deprecated conversion from string constant to 'char*'

Did I miss a correction?
>> Did I miss a correction?
No , my version of gcc doesn't give that warning so I don't see it. Look at them lines, if they contain char * change it to const char * or char const * (which ever you prefer they are semantically identical, I prefer the latter for reasons I don't go into here). This is just some versions of gcc being very thorough with checking const correctness (the warning is correct but is relatively benign and so probably safe to ignore).
Thanks. Is it ok if I leave this question open until I finish my overloaded << statements?

Ah, ok... the option for forcing this warning on in gcc is  -Wwrite-strings

Ok, so the problem is

       void setPurpose(char *,int);

needs to be

       void setPurpose(char const *,int);

Try this...
//Time.h
 
#include <iostream>
#ifndef TIME_H
#define TIME_H
class Time
{
private:
	int minutes;
	int hours;
	bool timeOfDay;
public:
	Time();
	Time(int,int,bool);
	int getMinutes();
	int getHours();
	void setMinutes(int);
	void setHours(int);
	void setTimeOfDay(bool);
	bool getTimeOfDay();
};
#endif
 
//Time.cpp
 
//#include "Time.h"
 
Time::Time()
{
	hours = 0;
	minutes = 0;
	//setTimeOfDay false;
	setTimeOfDay(false);
}
Time::Time(int hours1, int minutes, bool timeOfDay1)
{
	hours = hours1;
	minutes = minutes;
	timeOfDay = timeOfDay1;
}
int Time::getMinutes()
{
	return minutes;
}
 
void Time::setMinutes(int newMinutes)
{
	minutes = newMinutes;
}
 
int Time::getHours()
{
	return hours;
}
void Time::setHours(int newHours)
{
	hours = newHours;
}
bool Time::getTimeOfDay()
{
	return timeOfDay;
}
void Time::setTimeOfDay(bool newTimeOfDay)
{
	timeOfDay = newTimeOfDay;
}
 
 
//date.h
 
#ifndef DATE_H
#define DATE_H
#include <iostream>
 
class Date
{
private:
	int month;
	int day;
	int year;
public:
	Date();
	Date(int,int,int);
	int getMonth();
	void setMonth(int);
	int getDay();
	void setDay(int);
	int getYear();
	void setYear(int);
};
#endif
 
//Date.cpp
 
//#include "Date.h"
#include <iostream>
 
Date::Date()
{
	day = 0;
	month = 0;
	year = 0;
}
Date::Date(int day1, int month1, int year1)
{
	day = day1;
	month = month1;
	year = year1;
}
int Date::getMonth()
{
	return month;
}
 
void Date::setMonth(int newMonth)
{
	month = newMonth;
}
 
int Date::getDay()
{
	return day;
}
 
void Date::setDay(int newDay)
{
	day = newDay;
}
 
int Date::getYear()
{
	return year;
}
 
void Date::setYear(int newYear)
{
	year = newYear;
}
 
//DailyAppointmentScheduler.h
 
#include <iostream>
//#include "Date.h"
//#include "Time.h"
#ifndef DAILYAPPOINTMENTSCHEDULER_H
#define DAILYAPPOINTMENTSCHEDULER_H
 
class DailyAppointmentScheduler
{
private:
	static char  * person;
	char* purpose[10];
	Date calendarDate;
	Time calendarTime[10];
public:
	DailyAppointmentScheduler();
	DailyAppointmentScheduler(Date,Time);
	const char* getPerson() const;
	void setPerson(char*);
	const char* getPurpose() const;
	//void setPurpose(char**,int);
	void setPurpose(char const *,int);
	~DailyAppointmentScheduler();
	DailyAppointmentScheduler &operator=(const DailyAppointmentScheduler&);
	friend std::ostream& operator<<(std::ostream &os, const Date &d);
	friend std::ostream& operator<<(std::ostream &os, const Time &t);
};
#endif
 
//DailyAppointmentScheduler.cpp
 
//#include "DailyAppointmentScheduler.h"
//#include "Time.h"
//#include "Date.h"
 
#include <iostream>
 
using namespace std;
 
char * DailyAppointmentScheduler::person = NULL;
 
DailyAppointmentScheduler::DailyAppointmentScheduler()
{
}
DailyAppointmentScheduler::DailyAppointmentScheduler(Date,Time)
{
}
const char* DailyAppointmentScheduler::getPerson() const
{
	return person;
}
void DailyAppointmentScheduler::setPerson(char* newPerson)
{
	delete [] person;
	person = new char[strlen(newPerson+1)];
	strcpy(person,newPerson);
}
const char* DailyAppointmentScheduler::getPurpose() const
{
	//        return **purpose;
	return *purpose;
}
//void DailyAppointmentScheduler::setPurpose(char** newPurpose, int spot)
void DailyAppointmentScheduler::setPurpose(char const * newPurpose, int spot)
{
	purpose[spot] = new char[strlen(newPurpose)+1];
	//strcpy(purpose,newPurpose);
	strcpy(purpose[spot],newPurpose);
	delete [] newPurpose;
}
DailyAppointmentScheduler::~DailyAppointmentScheduler()
{
	//        delete [] purpose; <--- RX: You can't delete this as it's stack based storage
	delete person;
}
DailyAppointmentScheduler& DailyAppointmentScheduler::operator=(const DailyAppointmentScheduler& p)
{
	return *this;
}
ostream& operator << (ostream& os, const DailyAppointmentScheduler& s)
{
	//      os << s.getPerson() << endl;
	//      os << s.getDay() << " " << s.getMonth() << " " s.getYear() <<endl;
	//      os << s.getHours() << ":" << s.getMinutes() << " ";
	//      if(s.getAmOrPm() == false)
	//      {
	//              os << "am" << endl;
	//      }
	//      else
	//      {
	//              os <<"pm" << endl;
	//      }
	//      os << s.getPurpose() << endl;
 
	// You should return the stream
	return os;
}
 
//main.cpp
 
#include <iostream>
//#include "Date.h"
//#include "Time.h"
//#include "DailyAppointmentScheduler.h"
 
using namespace std;
 
int main ()
{
	//person = new char[10];
	char * person = new char[10];
 
	strcpy(person,"Paul");
	Date march2(3,2,2008);
	Date march3(3,3,2008);
	Date march4(3,4,2008);
	Date march5(3,5,2008);
	Date march6(3,6,2008);
	Date march7(3,7,2008);
	Date march8(3,8,2008);
	Time su[10];
	Time m[10];
	Time tu[10];
	Time w[10];
	Time th[10];
	Time f[10];
	Time sa[10];
	DailyAppointmentScheduler Sunday(march2,su[10]);
	DailyAppointmentScheduler Monday(march3,m[10]);
	DailyAppointmentScheduler Tuesday(march4,tu[10]);
	DailyAppointmentScheduler Wednesday(march5,w[10]);
	DailyAppointmentScheduler Thursday(march6,th[10]);
	DailyAppointmentScheduler Friday(march7,f[10]);
	DailyAppointmentScheduler Saturday(march8,sa[10]);
	Sunday.setPurpose("Go to jail.",0);
	su[0].setMinutes(30);
	su[0].setHours(12);
	su[0].setTimeOfDay(true);
	Sunday.setPurpose("Get out of jail.",1);
 
	// Undefined member
	// Sunday.setMinutes(45);
	su[1].setHours(6);
	su[1].setTimeOfDay(true);
	Monday.setPurpose("Brush Teeth", 9);
	m[9].setMinutes(30);
	m[9].setHours(7);
	m[9].setTimeOfDay(false);
	Monday.setPurpose("Go to sleep",3);
	m[3].setMinutes(45);
	m[3].setHours(8);
	m[3].setTimeOfDay(true);
	Tuesday.setPurpose("Go to class.",4);
	tu[4].setMinutes(50);
	tu[4].setHours(2);
	tu[4].setTimeOfDay(true);
	Wednesday.setPurpose("Read book.", 6);
	w[6].setMinutes(27);
	w[6].setHours(5);
	w[6].setTimeOfDay(true);
	Thursday.setPurpose("Take test.", 7);
	th[7].setMinutes(22);
	th[7].setHours(9);
	th[7].setTimeOfDay(false);
	Friday.setPurpose("Go to the club.",0);
	f[0].setMinutes(0);  
	f[0].setHours(7);  
	f[0].setTimeOfDay(true); 
	Saturday.setPurpose("Try to start program early.",0);
	sa[0].setMinutes(10); 
	sa[0].setHours(2);
	sa[0].setTimeOfDay(true);
 
	// You need this!
	delete [] person;
 
}

Open in new window

SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
>> although I am off to bed now
I'll leave you in I8's capable hands, he never sleeps ;)
I wish lol
Btw const did the trick
When I tried to implement my first overloaded << I get these strange compiler errors

In file included from DailyAppointmentScheduler.h:12,
                 from DailyAppointmentScheduler.cpp:10:
Time.h:34: error: declaration of 'operator<<' as non-function
Time.h:34: error: expected ';' before '(' token
In file included from Time.cpp:10:
Time.h:34: error: declaration of 'operator<<' as non-function
Time.h:34: error: expected ';' before '(' token
Time.cpp: In function 'std::ostream& operator<<(std::ostream&, Time&)':
Time.cpp:55: error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)os)), ((const char*)"Time: ")) << d->Time::getHours'
/usr/include/c++/4.2/ostream:112: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:121: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:131: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:169: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:173: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:177: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/bits/ostream.tcc:92: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:184: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/bits/ostream.tcc:106: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:195: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:204: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:208: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:213: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:217: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:225: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:229: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/bits/ostream.tcc:120: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
Time.cpp:53: note:                 std::ostream& operator<<(std::ostream&, Time&)
Time.cpp:56: error: invalid use of member (did you forget the '&' ?)
In file included from main.cpp:3:
Time.h:34: error: declaration of 'operator<<' as non-function
Time.h:34: error: expected ';' before '(' token




//time.h
 
#include <iostream>
#ifndef TIME_H
#define TIME_H
 
using namespace std;
 
class Time;
 
ostream &operator << (ostream & os, Time & d);
 
class Time
{
        private:
                        int minutes;
                        int hours;
                        bool timeOfDay;
        public:
                Time();
                Time(int,int,bool);
                int getMinutes();
                int getHours();
                void setMinutes(int);
                void setHours(int);
                void setTimeOfDay(bool);
                bool getTimeOfDay();
                friend ostream &operator << (os &, const Time &);
};
#endif
 
// time.cpp
#include "Time.h"
#include <iostream>
using namespace std;
 
Time::Time()
{
        hours = 0;
        minutes = 0;
        timeOfDay = false;
}
Time::Time(int hours1, int minutes, bool timeOfDay1)
{
        hours = hours1;
        minutes = minutes;
        timeOfDay = timeOfDay1;
}
int Time::getMinutes()
{
        return minutes;
}
 
void Time::setMinutes(int newMinutes)
{
        minutes = newMinutes;
}
 
int Time::getHours()
{
        return hours;
}
void Time::setHours(int newHours)
{
        hours = newHours;
}
bool Time::getTimeOfDay()
{
        return timeOfDay;
}
void Time::setTimeOfDay(bool newTimeOfDay)
{
        timeOfDay = newTimeOfDay;
}
 
ostream & operator << (ostream & os, Time& d)
{
        os << "Time: " << d.getHours << ":" << d.getMinutes << " ";
 if(d.getTimeOfDay == true)
        {
                os << "pm" << endl;
        }
        else
                os << "am" << endl;
        return os;
}       
 
 
 

Open in new window

Here's the fixed code - take a look at the comments :


//time.h
 
#include <iostream>
#ifndef TIME_H
#define TIME_H
 
using namespace std;
 
//class Time;                                          // <--- you don't need this here
 
//ostream &operator << (ostream & os, const Time & d); // <--- you don't need this here
 
class Time
{
        private:
                        int minutes;
                        int hours;
                        bool timeOfDay;
        public:
                Time();
                Time(int,int,bool);
                int getMinutes() const;                 // <--- make this const
                int getHours() const;                   // <--- make this const
                void setMinutes(int);
                void setHours(int);
                void setTimeOfDay(bool);
                bool getTimeOfDay() const;              // <--- make this const
                friend ostream &operator << (ostream &, const Time &);   // <--- ostream instead of os !!
};
#endif
 
 
 
// time.cpp
#include "Time.h"
#include <iostream>
using namespace std;
 
Time::Time()
{
        hours = 0;
        minutes = 0;
        timeOfDay = false;
}
Time::Time(int hours1, int minutes1, bool timeOfDay1)    // <--- minutes1 instead of minutes
{
        hours = hours1;
        minutes = minutes1;                   // <--- minutes1 instead of minutes
        timeOfDay = timeOfDay1;
}
int Time::getMinutes() const                  // <--- make this const
{
        return minutes;
}
 
void Time::setMinutes(int newMinutes)
{
        minutes = newMinutes;
}
 
int Time::getHours() const                    // <--- make this const
{
        return hours;
}
void Time::setHours(int newHours)
{
        hours = newHours;
}
bool Time::getTimeOfDay() const               // <--- make this const
{
        return timeOfDay;
}
void Time::setTimeOfDay(bool newTimeOfDay)
{
        timeOfDay = newTimeOfDay;
}
 
ostream & operator << (ostream & os, const Time& d)    // <--- don't forget the const
{
        os << "Time: " << d.getHours() << ":" << d.getMinutes() << " ";    // <--- don't forget the ()
 if(d.getTimeOfDay() == true)                          // <--- don't forget the ()
        {
                os << "pm" << endl;
        }
        else
                os << "am" << endl;
        return os;
}       

Open in new window

Ok corrected some things and got down to this.


Time.cpp: In function 'std::ostream& operator<<(std::ostream&, const Time&)':
Time.cpp:55: error: passing 'const Time' as 'this' argument of 'int Time::getHours()' discards qualifiers
Time.cpp:55: error: passing 'const Time' as 'this' argument of 'int Time::getMinutes()' discards qualifiers
Time.cpp:56: error: passing 'const Time' as 'this' argument of 'bool Time::getTimeOfDay()' discards qualifiers


I have no idea what that means.
Thanks infinity, making corrections. The book I am using recommends forward declarations and whatnot, why I put em in.
btw, it's best not to have "using namespace std;" in a header file. To avoid namespace pollution. Use the explicit namespace qualifier std:: where needed in header files.
What are the effects of namespace pollution?

ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Infinity, you beat me to my post. I had made all the corrections except to fix those before you posted. :)
>> What are the effects of namespace pollution?

I won't go into this a lot (you'll have to read up on namespaces if you want to know more ;) ), but namespaces exist to  to group symbols that logically belong together.
In different namespaces you can use the same name for a symbol, but with the namespace qualifier you can still distinguish them.

With using namespace std; you basically dump a whole namespace into the current namespace, which puts a lot of symbols from that namespace in the current namespace. What if there are doubles ? The chance of this happening increases if you put them in header files, where you have no control where the header will be included.
Ah good to know!
Ok now here comes my last question, that still has puzzled me since my last question. Now that I have 2 overloaded << operators for Date and Time, how do I incorporate them into my overloaded dailyapp operator? Since I didn't use inheritance I can't just say object.getMinutes() or object.getHours()

Is there a way to declare multiple objects in the header of an overloaded statement?
Since you have defined an operator<< for Time and Date, you can simply use it inside the operator<< for DailyAppointmentScheduler

      os << calendarDate;

will use the operator<< defined for the Date class to output the calendarDate.
Is there any way to display the name of my object? Or am I going to have to do some if and else statements to display the day of the week? :)
And I know I had said last question, lol.
>> Is there any way to display the name of my object? Or am I going to have to do some if and else statements to display the day of the week? :)

A date defines the day of the week exactly. For example, the 4th of march is a tuesday. You'll have to expand your Date class a bit to include retrieving the day of the week.

Note that the standard time.h include already provides a lot of date and time based functionality, which you can easily re-use :

        http://www.cplusplus.com/reference/clibrary/ctime/
I feel really really bad, but it is telling me none of the members were declared.

DailyAppointmentScheduler.cpp:58: error: 'calendarTime' was not declared in this scope
DailyAppointmentScheduler.cpp:60: error: 'hours' was not declared in this scope
DailyAppointmentScheduler.cpp:60: error: 'minutes' was not declared in this scope
DailyAppointmentScheduler.cpp:60: error: 'timeOfDay' was not declared in this scope


ostream& operator << (ostream& os, const DailyAppointmentScheduler& s)
{
        for(int i = 0; i <9;i++)
        {
                if( calendarTime[i] != '\0')
                {
                        os << hours <<":" << minutes << " " << timeOfDay << s.getPurpose() << endl;;
   
                }
        }

}

//Time.cpp

ostream & operator << (ostream & os, const Time & d)
{
        os << "Time: " << d.getHours() << ":" << d.getMinutes() << " ";
        if(d.getTimeOfDay() == true)
        {
                os << "pm" << endl;
        }
        else
                os << "am" << endl;
        return os;
}



 
   
//Date.cpp

ostream & operator << (ostream & os, const Date& d)
{
        os << "Date: " << d.getDay() << "/" << d.getMonth()<< "/" << d.getYear() <<  endl;
        return os;
}


>> DailyAppointmentScheduler.cpp:58: error: 'calendarTime' was not declared in this scope

Well, calendarTime is a member of the DailyAppointmentScheduler class, isn't it ? So, inside the operator<<, you'll need to use s.calendarTime to access it. Similarly for the other members.
I'm off to bed now for a few hours (need to get up in less than 5 hours again). I'll be back ;)
I am getting a memory leak when my program complies or something. I am getting a backtrace and memory map
excellent as usual. Thanks!
>> I am getting a memory leak when my program complies or something. I am getting a backtrace and memory map

Can you show that ? As well as your current code ?
Fixed the leak, I am just having trouble figuring out how to format my output now. I am going to try to combine everything into the overloaded << operator in DailyAppointmentScheduler.

However, even with friends, I am having plenty of trouble. I would like my output to be like this:

Sunday 9:00 am Go to school
              6:30 pm Go to Bed
Monday 8:30 am Go to school
              12:30 Go to dentist

.. so on

I am having trouble accessing members of the date array class objects. I think I will start another question on the problem.

>> I am having trouble accessing members of the date array class objects.

The point is that you don't have to do that - you can simply use the operator<< you already created for the Date class.

So, with an example :


class Test1 {
  private :
    int data;
  public :
    // other methods and constructors and ...
    friend std::ostream& operator<<(std::ostream &os, const Test1 &t1);
};
 
std::ostream& operator<<(std::ostream &os, const Test1& t1) {    // <--- the operator<< for the Test1 class
    os << t1.data;
    return os;
}
 
 
class Test2 {
  private :
    Test1 test1;          // <--- Test2 contains a Test1 object
  public :
    // other methods and constructors and ...
    friend std::ostream& operator<<(std::ostream &os, const Test2 &t2);
};
 
std::ostream& operator<<(std::ostream &os, const Test2& t2) {    // <--- the operator<< for the Test2 class
    os << t2.test1;           // <--- this will call the operator<< of the Test1 class for the test1 member
    return os;
}

Open in new window