c – 为什么我的运算符<<重载不工作?
发布时间:2020-12-16 09:28:26 所属栏目:百科 来源:网络整理
导读: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 iostr
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; } 时间与日期基本相同. 解决方法
您的操作符<<应该采用const引用(const Date&). 如果您的运算符采用非const引用,它们将无法使用临时对象(例如从Record :: date返回的对象).这就是造成错误的原因. 请注意,更改为const引用意味着您需要将所有调用的成员函数(例如Date :: month)更改为const.无论如何,这是一个好习惯. 另一种选择是通过值传递参数,这将调用复制构造函数. const引用通常是首选,因为它们通常更快,而且无论如何都不需要调用非const成员.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |