Ok, I have an assignment this week for school, and I thought I was all done, but ran into a small problem.
The assignment is to create a program that creates an event class with date and time objects. The sample output should be as follows:
New Year's day occurs at 01/01/2008 at 00:01
Valentine's Day occurs 2/14/2010 at 12:15
Here is my code as follows:
#include <iostream>
#include <string>
using namespace std;
class Time
{
public:
void setHour(int);
void setMinute(int);
int getHour();
int getMinute();
void printTime();
Time(); //default constructor
Time (int, int); //constructor with parameters
private:
int hour;
int minute;
};
Time::Time() //default constructor sets the hour and minute to 0
{
hour = 0;
minute = 0;
}
Time::Time(int hr, int min)
{
hr = hour;
min = minute;
}
class Date
{
public:
void setMonth(int);
void setDay(int);
void setYear(int);
int getMonth();
int getDay();
int getYear();
void printDate();
Date(); //default constructor
Date(int, int, int); //constructor with parameters
private:
int month;
int day;
int year;
};
Date::Date()
{
month = 1;
day = 01;
year = 2000;
}
Date::Date(int mmm, int dd, int yyyy)
{
mmm = month;
dd = day;
yyyy = year;
}
class Event
{
public:
Event(); //default constructor
Event(string, int, int, int, int, int); //constructor with parameters
string getName();
void printName();
void printDate();
void printTime();
private:
string eventName;
Time eventTime; //event object composed of a time object
Date eventDate; //event object composed of a date object
};
Event::Event()
{
}
Event::Event(string eDate, int eHour, int eMinute, int eMonth, int eDay, int eYear)
{
eventTime.setHour(eHour);
eventTime.setMinute(eMinute);
eventDate.setMonth(eMonth);
eventDate.setDay(eDay);
eventDate.setYear(eYear);
}
string Event::getName()
{
return eventName;
}
void Event::printName()
{
cout << eventName;
}
void Event::printDate()
{
cout << eventDate.getMonth() << "/" << eventDate.getDay() << "/" << eventDate.getYear();
}
void Event::printTime()
{
cout << eventTime.getHour() << ":" << eventTime.getMinute();
}
int main()
{
Event eventOne ("New Year's Day", 00, 01, 01, 01, 2010);
cout << eventOne.printName() << " occurs on " << eventOne.printDate() << " at " << eventOne.printTime() << "." << endl;
Event eventTwo ("Valentine's Day", 12, 15, 02, 14, 2010);
cout << eventTwo.printName() << " occurs on " << eventTwo.printDate() << " at " << eventTwo.printTime() << "." << endl;
cout << endl;
return 0;
}
I have 2 errors that I have been looking at for some time now and can not figure it out. Here are the errors:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
They are both found in the main().
If there is anyone that could help me out, I would greatly appreciate it.
Change the member functions so that they return std::strings, and instead of using std::cout in them, just return the text they were supposed to print. This way, you will not need to change main.
I guess that I have been staring at this screen way too long this evening. I can't see what you mean. Maybe I just need to take a break and come back and look at it.
Alright, but when you do, here's something to consider.
What's the return type of your member function printName()? void.
Can void be on the right side of the << operator? No.
What can be on the right side of the << operator? An std::string.
Therefore, what can you make your member function return? See the previous answer.
More specifically, what can you make your member function return? eventName.
However, if you leave the std::cout statement in the function, will the data you want printed end up getting printed twice? You bet.
Does this help you out? I don't know. You tell me. ;)
You'll also need to fix printDate() and printTime().
Ok, I tried a few different things and i'm returning different errors now. Here is the code:
#include <iostream>
#include <string>
using namespace std;
class Time
{
public:
void setHour(int);
void setMinute(int);
int getHour();
int getMinute();
void printTime();
Time(); //default constructor
Time (int, int); //constructor with parameters
private:
int hour;
int minute;
};
Time::Time() //default constructor sets the hour and minute to 0
{
hour = 0;
minute = 0;
}
Time::Time(int hr, int min)
{
hr = hour;
min = minute;
}
class Date
{
public:
void setMonth(int);
void setDay(int);
void setYear(int);
int getMonth();
int getDay();
int getYear();
void printDate();
Date(); //default constructor
Date(int, int, int); //constructor with parameters
private:
int month;
int day;
int year;
};
Date::Date()
{
month = 1;
day = 01;
year = 2000;
}
Date::Date(int mmm, int dd, int yyyy)
{
mmm = month;
dd = day;
yyyy = year;
}
error C2297: '<<' : illegal, right operand has type 'const char [2]'
error C2297: '<<' : illegal, right operand has type 'const char [2]'
warning C4552: '<<' : operator has no effect; expected operator with side-effect
error C2297: '<<' : illegal, right operand has type 'const char [2]'
warning C4552: '<<' : operator has no effect; expected operator with side-effect
error C2296: '<<' : illegal, left operand has type 'void'
error C2297: '<<' : illegal, right operand has type 'const char [12]'
error C2296: '<<' : illegal, left operand has type 'void'
error C2297: '<<' : illegal, right operand has type 'const char [12]'
As you can tell, I am completely new at this and I feel like I am botching up this assignment. Any help would be great.