0
votes

I have created _year,_day,_date as private integers in class Date.
error: `_year' is not a type and similar for day and mon in write()
error: ambiguous overload for 'operator>>' in 'istr >> ((const oop244::Date*)this)->oop244::Date::_year'

note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>&(*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits]

std::ostream& Date::write(std::ostream& ostr) const
{
    int year,mon,day;
    return ostr<<year<< "/" <<mon<< "/" <<day;
    this._year=year;
    this._mon=mon;
    this._day=day;        
}

//overloading istream for object Date input
std::istream& Date::read(std::istream& istr)const
{ 
    char c;
    istr>>_year>>c>>_mon>>c>>_day;// char c for '/'
    if(istr.fail())
        _readErrorCode=1;
    this->validate();
    return istr;
}

//overloading  << operator
std::ostream &operator<<(std::ostream& ostr,Date &d)
{
    return d->write(ostr);
}

//overloading  >> operator
std::istream& operator>>(Date &d,std::istream& istr)
{
    return d->read(istr);
}
2
The rules for overloading make this ambiguous, and that's what the compiler is complaining about. You need to mark the constructor as explicit. - Vinay Shukla
yanivx can you please explain more. - shilpa mishra
Couple things: 1. You have an misplaced }. And unless you have the oddest operator-> overload in class Date, those d-> should be d.. Perhaps post the real code? - WhozCraig
when i use d. instead of d-> it still gives error - shilpa mishra

2 Answers

0
votes

The overloaded << operator function must then be declared as a friend of class Date so it can access the private data within a Date object.

friend std::ostream &operator<<(std::ostream& ostr,Date &d)
friend std::ostream &operator<<(std::ostream& ostr,Date &d)

Here is an example which you can use for reference.

Link to similar example

0
votes

Your description - frankly - is quite poor, since you haven't included all relevant code (e.g. the class definition), have included code not relevant to your problem, and have given a sloppy summary of error messages from your compiler when copying the actual error messages would have been informative.

It is obvious that you are just typing in code at random, and then wondering why it doesn't work. And your description relies on people here being mindreaders, which most people are not.

However, I'll give a few pointers to some of the problems in your code.

One is in the definition of operator<<()

std::ostream &operator<<(std::ostream& ostr,Date &d)
{
  return d->write(ostr);
}

In this, d is a reference, but d->write(ostr) treats it like a pointer. That is invalid. Use d.write(ostr) instead. Similarly in operator>>().

Another problem is in your Date::write()

std::ostream& Date::write(std::ostream& ostr) const
{
    int year,mon,day;
    return ostr<<year<< "/" <<mon<< "/" <<day; 
    this._year=year;
    this._mon=mon;
    this._day=day;
}

The first statement creates three local variables, year, month, and day. They are not initialised before writing them out. The three lines starting at this._year=year are not even reached because the function returns. this is also a pointer, so the . operator is invalid. The function actually needs to write out the values (presumably declared as members of Date) of _year, _mon, and _day. But doesn't.