为什么我的运营商<<超载不工作?

时间:2010-06-21 00:40:42

标签: c++ operators

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;
}

时间与日期基本相同。

2 个答案:

答案 0 :(得分:4)

您的operator<<应该使用const引用(const Date &)。

如果您的运算符采用非const引用,它们将无法使用临时对象(例如从Record::date返回的对象)。这就是造成错误的原因。

请注意,更改为const引用意味着您需要将所有被调用的成员函数(例如Date::month)更改为const。无论如何,这是一种很好的做法。

另一种选择是通过值传递参数,这将调用复制构造函数。 const引用通常是首选,因为它们通常更快,而且无论如何都不需要调用非const成员。

答案 1 :(得分:3)

因为阅读操作员需要对变量进行“只读”访问:

ostream &operator<<(ostream &out, const Record &record) //<< const

ostream &operator<<(ostream &out, const Date &date) //<< const

以下是有关成员函数constness的一些解释,因为您必须确保某些访问器是const:http://www.parashift.com/c++-faq-lite/const-correctness.html