//Use this file in unmodified form with the following exceptions:
//If you used class names other than "Date" and "FutureDate", you may modify those
// lines to reflect your class names
//If you are using an operating system that does not support the system("pause")
// function, you may remove it or replace it with a cin statement.
//Please note: while testing, you will probably want to comment out portions of this
// code to make sure your class is working. However, the submitted classes should
// run using the entire file (with possible modifications above).
#include <cstdlib>
#include <iostream>
#include "Date.h"
#include "FutureDate.h"
usingnamespace std;
int main()
{
int menuChoice; //used for menu
int tempMonth;
int tempDay;
int tempYear;
cout<<"Creating Date object with default constructor..."<<endl;
Date date1;
cout<<"Stream insertion operator..."<<endl;
cout<<date1<<endl;
cout<<"\nStream extraction operator..."<<endl;
cin>>date1;
cout<<"Stream insertion operator..."<<endl;
cout<<date1<<endl;
//Ask user to enter data for next 2 objects
cout<<"\n\nYou will be asked to enter a date to be used for the next 2 object."<<endl;
cout<<"Try entering months and days outside the valid range."<<endl;
cout<<"This program does not try to validate leap years."<<endl;
cout<<"\n Enter a month: ";
cin>>tempMonth;
cout<<" Enter a day: ";
cin>>tempDay;
cout<<" Enter a year:";
cin>>tempYear;
cout<<"\nCreating a Date object with the overloaded constructor..."<<endl;
Date date2(tempMonth, tempDay, tempYear);
cout<<"Stream insertion operator..."<<endl;
cout<<date2<<endl;
cout<<"\nCreating a FutureDate object using the overloaded constructor..."<<endl;
FutureDate aFutureDate(tempMonth, tempDay, tempYear);
cout<<"Stream insertion operator..."<<endl;
cout<<aFutureDate<<endl;
cout<<"\n\nYou will now use the in/decrement operators on the FutureDate"<<endl;
cout<<"Press (1) to use the increment operator"<<endl;
cout<<"Press (2) to use the decrement operator"<<endl;
cout<<"Press (0) to exit"<<endl;
cin>>menuChoice;
while(menuChoice)
{
switch(menuChoice)
{
case 1:
aFutureDate++;
cout<<aFutureDate<<endl;
break;
case 2:
aFutureDate--;
cout<<aFutureDate<<endl;
}
cout<<"Press (1) to use the increment operator"<<endl;
cout<<"Press (2) to use the decrement operator"<<endl;
cout<<"Press (0) to exit"<<endl;
cin>>menuChoice;
}
system("PAUSE");
return EXIT_SUCCESS;
}
class Date
{
private:
int month;
int day;
int year;
public:
Date();
Date()
{
month = 12;
day = 25;
year = 2005;
};
Date(int m, int d, int y)
{
month = m;
day = d;
year = y;
};
};
My issue is that every time the driver program uses objects of Date in cin or cout. I get:
Error no operator matches these operands. I googled and found that I have to overload the constructor:
but I can't figure out how
could somebody please give an example for this case?
If you want to output a custom data type with cout, or populate one with cin, you must provide proper overloads for the << or >> operators respectively.
For example, this operator<< overload will allow you to output a custom struct with cout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
struct Data
{
int x, y;
}
// Global operator<< overload:
std::ostream &operator<<(std::ostream &os, const Data &d)
{
os << "(" << d.x << ", " << d.y << ")";
return os;
}
//Now this will work:
Data someData;
std::cout << someData << std::endl;
I commented most of the driver.cpp out as I began to understand "what it what" This way I don't get confused with later problems. All I'm dealing with right now is
1 2 3 4 5 6 7 8
cout<<"Creating Date object with default constructor..."<<endl;
Date date1;
cout<<"Stream insertion operator..."<<endl;
cout<<date1<<endl;
cout<<"\nStream extraction operator..."<<endl;
cin>>date1;
cout<<"Stream insertion operator..."<<endl;
cout<<date1<<endl;
I have tried to implement Thumpers code into my header file .. not excactly succesfull... so I kept researching and found out about templates
I adjusted Thumpers code snippet so it fits mine without syntax errors.
like I stated in the original post the .cpp can't be changed so it hall has to happen inside the header file.
I have messed with this all night and finally got Visual Studio to look happy "Syntax wise"