Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我的操作员<&书信电报;超载不起作用? 错误: ..\Record.cpp:在函数`std::ostream&operator_C++_Operators - Fatal编程技术网

C++ 为什么我的操作员<&书信电报;超载不起作用? 错误: ..\Record.cpp:在函数`std::ostream&operator

C++ 为什么我的操作员<&书信电报;超载不起作用? 错误: ..\Record.cpp:在函数`std::ostream&operator,c++,operators,C++,Operators,中,您的operator,因为读取运算符需要对变量进行“只读”访问: /* * 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 = 1

中,您的
operator,因为读取运算符需要对变量进行“只读”访问:

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

ostream&operator为什么我现在会出现此错误\Date.cpp:在函数
std::ostream&operator@cactusbin:您需要将month函数定义为:
unsigned int Date::month()const{return\u month;}
。并在头文件中的函数声明之后添加
const
/*
 * 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
 *
 *  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;
}
ostream &operator<<(ostream &out, const Record &record) //<< const

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