简体   繁体   English

C ++ istream >>和ostream <<错误

[英]C++ istream >> and ostream << errors

I have created _year,_day,_date as private integers in class Date. 我已经在Date类中将_year,_day,_date创建为私有整数。
error: `_year' is not a type and similar for day and mon in write() 错误:“ _ year”不是类型,在write()中与day和mon类似
error: ambiguous overload for 'operator>>' in 'istr >> ((const oop244::Date*)this)->oop244::Date::_year' 错误:'istr >>((const oop244 :: Date *)this)-> oop244 :: Date :: _ year'中'operator >>'的模棱两可的重载

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

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. 然后,必须将重载的<<操作符函数声明为Date类的朋友,以便它可以访问Date对象内的私有数据。

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 链接到类似的例子

Your description - frankly - is quite poor, since you haven't included all relevant code (eg 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<<() 一种是在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. 在这种情况下, d是引用,但是d->write(ostr)将其视为指针。 That is invalid. 那是无效的。 Use d.write(ostr) instead. 请改用d.write(ostr) Similarly in operator>>(). 在运算符>>()中类似。

Another problem is in your Date::write() 另一个问题是您的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 . 第一条语句创建三个局部变量, yearmonthday 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._year=year开始的this._year=year this is also a pointer, so the . this也是一个指针,因此. operator is invalid. 运算符无效。 The function actually needs to write out the values (presumably declared as members of Date ) of _year , _mon , and _day . 该函数实际上需要写出_year_mon_day的值(可能声明为Date成员)。 But doesn't. 但是没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM