我的类成员函数中没有匹配'operator >>',使用set并读取文件输入

问题描述 投票:0回答:2

因此,我试图创建一个函数,该函数利用我的set和get方法将文件中的输入读取到我的类中。我必须从以下方法中删除友谊:friend istream & operator>>(istream & input, Date & D,并使用我自己的get和set方法。这是我到目前为止在类中遇到的错误,Date.h

#if !defined(_DATE_H)
#define _DATE_H

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>

using namespace std;

class Date {
public:
    Date();
    Date(unsigned day1, string month1, unsigned year1);

    void SetDay(unsigned day1);
    void SetMonth(string month1);
    void SetYear(unsigned year1);
    unsigned GetDay() const;
    string GetMonth() const;
    unsigned GetYear() const;

    void SetDate(istream &input, Date & D);
    void GetDate(ostream & os, Date & D);


private:

    unsigned day;
    string month;
    unsigned year;

};

 ostream & operator <<(ostream & os, Date & D);
 istream & operator >>(istream & input, const Date & D);
#endif  //_DATE_H

这是我的.cpp文件:

//
//
//  Generated by StarUML(tm) C++ Add-In


#include "Date.h"

Date::Date(unsigned day1, string month1, unsigned year1) {

    day = day1;
    month = month1;
    year = year1;

}


void Date::SetDay(unsigned day1) {

    day = day1;

}

void Date::SetMonth(string month1) {

    month = month1;

}

void Date::SetYear(unsigned year1) {

    year = year1;

}

unsigned Date::GetDay() const {

    return day;

}

string Date::GetMonth() const {

    return month;

}

unsigned Date::GetYear() const {

    return year;

}
istream & operator >>( istream & input, Date & D) {

    D.SetDate(input,D);
    return input;

}

ostream & operator <<( ostream & os, Date & D) {

    D.GetDate(os,D);

    return os;

}
void Date::SetDate(istream &input, Date & D){

    input >> D.SetDay(day) >> D.SetMonth(month) >> D.SetYear(year);
    input.ignore();

}

void Date::GetDate(ostream &os, Date & D){

    os << "  Date: " << D.GetDay() << " " << D.GetMonth() << " " << D.GetYear() << '\n';

}

我收到错误error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'void')|。这是什么意思,我该如何解决?

c++ class get set operator-keyword
2个回答
1
投票

声明

input >> D.SetDay(day) >> D.SetMonth(month) >> D.SetYear(year);

没有意义,因为函数不返回任何东西。

解决问题的一种方法是创建临时变量(例如daymonthyear)并读取它们。然后分别调用传递变量的set函数。

另外,请不要在输入函数中执行input.ignore()(也不要在输出函数中打印换行符,这取决于输入/输出函数的用户来处理。


0
投票

您使用的方法错误。

void Date::SetDate(istream &input, Date & D){

    input >> D.SetDay(day) >> D.SetMonth(month) >> D.SetYear(year);
    input.ignore();

}

D.SetDay返回void,不能那样使用。您可以像这样修复它:

void Date::SetDate(istream &input, Date & D){
    unsigned day_in;
    string month_in;
    unsigned year_in;
    input >> day_in >> month_in >> year_in;
    D.SetDay(day_in);
    D.SetMonth(month_in);
    D.SetYear(year_in);
}

但是,有理由这么冗长。由于SetDate是成员函数,因此不需要传递Date对象,它可以直接访问私有成员:

void Date::SetDate(istream &input){
    input >> day >> month >> year;
}

更惯用的方式是提供>>重载作为自由函数:

std::istream& (std::istream &in, Date& d){
    input >> d.day >> d.month >> d.year;
}

但是您需要将其声明为friendDay,以便它可以访问私有成员。

© www.soinside.com 2019 - 2024. All rights reserved.