thedafferg
thedafferg

Reputation: 99

No match for 'operator>>' in my class member function, using set and get to read file input

So im trying to create a function that makes use of my set and get methods to read input from a file, into my class. I have to remove friendship from the following method: friend istream & operator>>(istream & input, Date & D and make use of my own get and set methods. This is what I have so far in the class I get the error in, 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

And this is my .cpp file:

//
//
//  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';

}

I get the error error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'void')|. What does this mean and how can I fix it?

Upvotes: 0

Views: 109

Answers (2)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122228

You are using your methods wrong.

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

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

}

D.SetDay returns void and cannot be used like that. You could fix it like this:

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

However, there is reason to be that verbose. As SetDate is a member function it does not need to get passed a Date object and it can access private members directly:

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

The more idomatic way is to provide an >> overload as free function:

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

But you need to declare it as friend of Day so it can access private members.

PS: Only after writing this I noticed that you already have the overload as free function. Instead of making that call SetDate it can implement the input directly. Your SetDate does not add anything useful.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409166

The statement

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

makes no sense, since the functions doesn't return anything.

One way to solve your problem is to create temporary variables (e.g. day, month and year) and read into them. Then call the set functions separately passing the variables.

On another note, don't do input.ignore() in your input function (and don't print the newline in the output function), it's up to the user of the input/output functions to handle that.

Upvotes: 3

Related Questions