0

根据要求,实际文件:

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

我做了一个类似于以下的简单程序,我得到了类似的错误。

班级:

#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 

和日期.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

驱动程序.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.

更长的版本:(忽略以下内容)当我使用以下内容时,我会收到可怕的警告:

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 的调试页面中指定。我在 >> 之后使用不同的变量重复以下内容。据我所知,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

};

和:

#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 的重载代码,但不会导致错误。

4

1 回答 1

2

operator >>编译器告诉您while存在问题trying to match the argument list '(std::istream, date)'。事实上,在您的班级重载中operator >>,您date在发布的代码的最后一个版本中有此说明:

istr >> "5";

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

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

于 2013-02-02T23:13:35.333 回答