istream超载无法正常工作

时间:2013-02-02 21:51:53

标签: c++ overloading istream

根据要求,实际档案:

d_date.h http://pastebin.com/AFe4XE2c

d_except.h http://pastebin.com/8QE2m8ia

d_date.cpp http://pastebin.com/dgpxLWKv

input.dat http://pastebin.com/XUpRcu9E

我做了一个类似于下面的裸骨程序,我也遇到了类似的错误。

CLASS:

#ifndef DATE_CLASS
#define DATE_CLASS

#include <iostream>
#include <iomanip>
#include <string>

//#include "d_except.h"

using namespace std;

class date
{
   public:
      date ();

      //ADDED
      friend ostream& operator<<(ostream&, const date&); 
      friend istream& operator>>(istream&, date&);

   private:
      int month, day, year;
            // private members that specify the date

};


#endif 

AND DATE.CPP:

#ifndef DATE_CPP
#define DATE_CPP

#include "date.h"
#include <stdexcept>
#include <iostream>

using namespace std;

ostream& operator<<(ostream& ostr, const date& date1){
     char blah;
     ostr << blah;
 }

 istream& operator>>(istream& istr, date& date1){
     istr >> "5";
     return istr;
 }


#endif

DRIVER.CPP:

#include <iostream>
#include "date.h"
//#include "d_except.h"

using namespace std;

void main (void)
{
    date date1;
    date date2;
}

和错误(不是发布长度列表,而是发布到最后的列表:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(373): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned __int64 &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(392): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(float &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(411): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(double &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(429): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long double &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(447): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(void *&)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(466): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_streambuf<_Elem,_Traits> *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          while trying to match the argument list '(std::istream, const char [2])'
1>  Generating Code...
1>
1>Build FAILED.

LENGTHIER版本: (以下是IGNORE) 当我使用以下内容时,我会收到可怕的警告:

class date
{
   public:
    //irrelevant (hopefully) functions not shown are here
    friend ostream& operator<<(ostream&, const date&); 
    friend istream& operator>>(istream&, date&);

   private:
    int month, day, year;
}

 ostream& operator<<(ostream& ostr, const date& date1){
    ostr << date1.getDay() << "/" << date1.getMonth() << "/" << date1.getYear() << " "; 
    return ostr;
 }

 istream& operator>>(istream& istr, date& date1){
     int d, m, y;
     char ch;
     istr >> d >> ch >> m >> ch >> y >> ch;
     date1.setDay(d);
     date1.setMonth(m);
     date1.setYear(y);
     return istr;
 }

我的输入是input.dat,并在Microsoft Visual Studio 2010的DeBugging页面中指定。在&gt;&gt;之后,我会使用不同的变量重复以下内容。我可以说,使用date的istream存在一些问题。我认为在这篇文章的最后一行“告诉我”问题是什么,但我仍然没有得到它。

c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(411): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(double &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(429): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long double &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(447): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(void *&)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(466): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_streambuf<_Elem,_Traits> *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          while trying to match the argument list '(std::istream, date)'

运行此东西的实际.cpp是:

void main (void)
{
    date date1;
    date date2;

    cout << "Enter date1 and date2:" << endl;

    while (cin >> date1 >> date2)
    {
        cout << "Printing date1 and date2" << endl;
        cout << date1 << endl << date2 << endl;
                //and more...

}

附录:

class date
{
   public:
      date (int mm=1, int dd=1, int yyyy=1900);
            // supply date in format MM/DD/YYYY
            // preconditions: 1 <= mm <= 12,
            //                1 <= dd <= daysInMonth()

      void writeShortDate () const;
            // output the date in the format "MM/DD/YYYY"
      void writeLongDate () const;
            // output the date in the format "month day, year"

      void incrementDate(int ndays);
            // add ndays days to the date
            // precondition: 0 <= ndays <= 365

      int numberOfDays() const;
            // return the number of days into the year

      int getMonth() const;// done
            // return the month as integer value 1 to 12
      int getDay() const; //done
            // return day of the month
      int getYear() const;  //done
            // return the year

      void setMonth(int mm); //done
            // update the month
            // precondition: 1 <= mm <= 12
      void setDay(int dd); //done
            // update the day
            // precondition: 1 <= dd <= daysInMonth()
      void setYear(int yyyy); //done
            // update the year
            // precondition:  if the date is February 29,
            //                yyyy must be a leap year
      int daysInMonth() const;
            // return number of days in the month

      bool isLeapYear() const;
            // is the current year a leap year (true/false)

      //ADDED
      bool operator< (const date&) const;
      bool operator> (const date&) const;
      date operator++ ();
      friend ostream& operator<<(ostream&, const date&); 
      friend istream& operator>>(istream&, date&);
   private:
      enum monthName {Jan = 1, Feb, Mar, Apr, May, Jun,
                      Jul, Aug, Sep, Oct, Nov, Dec};
            // private type used by date

      int month, day, year;
            // private members that specify the date

};

AND:

#ifndef DRIVER_H
#define DRIVER_H

#include <iostream>
#include "d_date.h"
#include "d_except.h"

using namespace std;

void main (void)
{
    date date1;
    date date2;

    cout << "Enter date1 and date2:" << endl;

    while (cin >> date1 >> date2)
    {
        cout << "Printing date1 and date2" << endl;
        cout << date1 << endl << date2 << endl;

        if (date1 == date2) 
            cout << date1 << " is equal to " << date2 << endl;

        if (date1 != date2) 
            cout << date1 << " is not equal to " << date2 << endl;

        if (date1 < date2) 
            cout << date1 << " is less than " << date2 << endl;

        if (date1 > date2) 
            cout << date1 << " is greater than " << date2 << endl;

        ++date1;
        ++date2;

        cout << "Increment of date1: " << date1 << endl;
        cout << "Increment of date2: " << date2 << endl;

        cout << endl << "---End of Run---" << endl << endl;

        cout << "Enter date1 and date2:" << endl;
    }


}

这是针对学校的,我讨厌发布几乎整个作业,但我无法修复错误。以上是date.h和driver.cpp的更详细版本。

我不知道我做了什么,但现在唯一的错误是:

Error   1   error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)

另外,忘了提及......这里没有显示driver.cpp的重载代码,但没有导致错误。

1 个答案:

答案 0 :(得分:1)

编译器告诉您operator >> trying to match the argument list '(std::istream, date)'时出现问题。实际上,在operator >>类的date重载中,您在最后一个版本的代码中有了这条指令:

istr >> "5";

通常,operator >>重载签名中的第二个参数是对应该从流中提取(反序列化)的对象的非const引用。但是,字符串文字"5"的类型为char [2],并且该类型的operator >>不存在重载。这是有道理的:你如何从流中提取对象并将其存储到字符串文字中?

因此,你得到的错误。我怀疑您之前版本的代码受到类似问题的影响。