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 

および 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

ドライバー.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 >>オーバーロードのシグネチャの 2 番目のパラメーターは、ストリームから抽出 (逆シリアル化) されるオブジェクトへの非 const 参照です。ただし、文字列リテラル"5"には型があり、その型char [2]のオーバーロードは存在しませんoperator >>。どうすればストリームからオブジェクトを抽出し、それを文字列リテラルに格納できるでしょうか?

したがって、エラーが発生します。以前のバージョンのコードも同様の問題の影響を受けたと思われます。

于 2013-02-02T23:13:35.333 に答える