[C ++朋友运算符重载cin >>

问题描述 投票:0回答:1

我正在用朋友运算符替换读入功能。我在void函数中引用朋友运算符时遇到麻烦。我在无效的GetDates函数中收到错误“在日期中没有名为“读取”的成员”的错误。有谁知道如何解决这一问题?谢谢!

class Date {
private:
    int mn;        //month component of a date
    int dy;        //day component of a date
    int yr;        //year comonent of a date

public:
    //constructors
    Date() : mn(0), dy(0), yr(0)
    {}
    Date(int m, int d, int y) : mn(m), dy(d), yr(y)
    {}

    //input/output functions
    friend istream& operator>>(istream& Read, Date& d);   //overload friend Read
    void Write() const;                                   
    void GetDates();
    void Sort();
};

//Date class member functions

istream& operator >>(istream& Read, Date& d) //**NEED TO REPLACE with overload vs as friends to Date function**
{
    char skip_char;

    Read >> d.mn >> skip_char >> d.dy >> skip_char >> d.yr;
    return Read;
}


void GetDates(Date l[], int &n)      //reads list l, and returns count in n
   {

       cout << "How many date values are to be processed (1 - 100)? ";
       cin >> n;

       while ((n < 0) || (n > 100)) {
           cout << "Invalid value; enter number between 0 and 100: ";
           cin >> n;
       }

       for (int i = 0; i < n; i++) {
           cout << "Enter a date (mm/dd/yyyy): ";
           l[i].Read(); //ERROR HERE
       }
   }
c++ operator-overloading
1个回答
0
投票

Read是要从中提取stream的名称。您可以读取的流的一个示例是cin。您需要替换此行:

l[i].Read(); //ERROR HERE

cin >> l[i];

operator>>内部,cin对象现在称为Read

© www.soinside.com 2019 - 2024. All rights reserved.