Why is my operator<< overloading not working?
Error:
..\Record.cpp: In function `std::ostream& operator<<(std::ostream&, Record&)':
..\Record.cpp:83: error: no match for 'operator<<' in 'out << Record::date()()'
Record.cpp:
/*
* Record.cpp
*
* 开发者_如何转开发 Created on: Jun 13, 2010
* Author: DJ
*/
#include <iostream>
#include "Record.h"
using std::string;
using std::istream;
using std::ostream;
Record::Record() {
}
Record::Record(Date inDate) {
_date = inDate;
}
Record::Record(Date inDate, Time inTime) {
_date = inDate;
_time = inTime;
}
Record::Record(Date inDate, Time inTime, string inDescription) {
_date = inDate;
_time = inTime;
_description = inDescription;
}
Record::~Record() {
}
Time Record::time() {
return _time;
}
void Record::time(Time inTime) {
_time = inTime;
}
Date Record::date() {
return _date;
}
void Record::date(Date inDate) {
_date = inDate;
}
string Record::description() {
return _description;
}
void Record::description(string inDescription) {
_description = inDescription;
}
void Record::operator=(Record record) {
_date = record.date();
_time = record.time();
_description = record.description();
}
istream &operator>>(istream &in, Record &record) {
Time inTime;
Date inDate;
string inDescription;
in >> inDate >> inTime >> inDescription;
record.date(inDate);
record.time(inTime);
record.description(inDescription);
return in;
}
ostream &operator<<(ostream &out, Record &record) {
out << record.date() << " " << record.time() << " " << record.description();
return out;
}
Date.cpp:
/*
* Date.cpp
*
* Created on: Jun 13, 2010
* Author: DJ
*/
#include "Date.h"
#include <iostream>
using std::istream;
using std::ostream;
Date::Date() {
_day = 1;
_month = 1;
_year = 1999;
}
Date::Date(unsigned int inDay) {
day(inDay);
_month = 1;
_year = 1999;
}
Date::Date(unsigned int inDay, unsigned int inMonth) {
day(inDay);
month(inMonth);
_year = 1999;
}
Date::Date(unsigned int inDay, unsigned int inMonth, unsigned int inYear) {
day(inDay);
month(inMonth);
year(inYear);
}
Date::~Date() {
}
void Date::day(unsigned int inDay) {
assert(inDay <= daysInMonth());
_day = inDay;
}
unsigned int Date::day() {
return _day;
}
void Date::month(unsigned int inMonth) {
assert(inMonth <= 12);
_month = inMonth;
}
unsigned int Date::month() {
return _month;
}
void Date::year(unsigned int inYear) {
_year = inYear;
}
unsigned int Date::year() {
return _year;
}
void Date::operator=(Date date) {
day(date.day());
month(date.month());
year(date.year());
}
istream &operator>>(istream &in, Date &date) {
char dummy;
unsigned int day, month, year;
in >> month >> dummy >> day >> dummy >> year;
date.day(day);
date.month(month);
date.year(year);
return in;
}
ostream &operator<<(ostream &out, Date &date) {
out << date.month() << "/" << date.day() << "/" << date.year();
return out;
}
unsigned int Date::daysInMonth() {
if(_month == 1 || _month == 3 || _month == 5 || _month == 7 || _month == 8 || _month == 10 || _month == 12)
return 31;
else
return 30;
}
Time is basically the same as date.
Your operator<<
should take const references (const Date &
).
If your operators take non-const references, they won't work with temporary objects (such as the one returned from Record::date
). This is what's causing the error.
Note that changing to const references means you will need to change any member functions called (e.g. Date::month
) to be const. This is good practice anyway.
Another option is to pass the parameter by value, which will invoke the copy constructor. const references are usually preferred because they are generally faster, and you shouldn't need to invoke non-const members anyway.
Because the reading operator needs "read-only" access to the variable :
ostream &operator<<(ostream &out, const Record &record) //<< const
ostream &operator<<(ostream &out, const Date &date) //<< const
Here are some explanations about member function constness, as you'll have to make sure some accessors are const : http://www.parashift.com/c++-faq-lite/const-correctness.html
精彩评论