Why is my operator << overload 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;
}

The time is basically the same as the date.

+3
source share
2 answers

Your operator<<should accept const ( const Date &) links .

If your operators accept non-constant references, they will not work with temporary objects (for example, returned from Record::date). This causes an error.

Note that changing the references to const means that you will need to change any member functions called (e.g. Date::month) as const. In any case, this is a good practice.

- , . const , , , , .

+4

"read-only" :

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

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

-, , : http://www.parashift.com/c++-faq-lite/const-correctness.html

+3

Source: https://habr.com/ru/post/1750943/


All Articles