C++ 友元运算符重载 cin>>
C++ friend operator overloading cin>>
我正在用友元运算符替换读入函数。我在 void 函数中引用友元运算符时遇到问题。我在 void GetDates 函数中收到错误 "No member named 'Read' in Date"。有谁知道如何解决这一问题?谢谢!
问题二:
我现在正在使用 cout<< 运算符,但我的变量收到错误消息:“'mn' 是 'Date' 的私有成员”
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
friend ostream& operator<<(istream& write, Date& d); //overload friend write
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
}
}
ostream& operator <<(ostream& write, Date& d) //friend write
{
if (d.mn < 10)
cout << '0';
cout << d.mn << '/';
if (d.dy < 10)
cout << '0';
cout << d.dy << '/';
if (d.yr < 1000)
cout << '0';
if (d.yr < 100)
cout << '0';
if (d.yr < 10)
cout << '0';
cout << d.yr;
return write;
}
Read
是您要从中提取的 stream
的名称。您可以读取的流示例是 cin
。您需要替换此行:
l[i].Read(); //ERROR HERE
和
cin >> l[i];
在 operator>>
中,cin
对象现在称为 Read
。
您的 operator<<
的问题是它需要声明为 friend
,就像您对 operator>>
所做的那样。
此外,您正在写入 cout
而不是写入 write
。一旦您尝试写入任何其他流,这将不起作用。
我正在用友元运算符替换读入函数。我在 void 函数中引用友元运算符时遇到问题。我在 void GetDates 函数中收到错误 "No member named 'Read' in Date"。有谁知道如何解决这一问题?谢谢!
问题二: 我现在正在使用 cout<< 运算符,但我的变量收到错误消息:“'mn' 是 'Date' 的私有成员”
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
friend ostream& operator<<(istream& write, Date& d); //overload friend write
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
}
}
ostream& operator <<(ostream& write, Date& d) //friend write
{
if (d.mn < 10)
cout << '0';
cout << d.mn << '/';
if (d.dy < 10)
cout << '0';
cout << d.dy << '/';
if (d.yr < 1000)
cout << '0';
if (d.yr < 100)
cout << '0';
if (d.yr < 10)
cout << '0';
cout << d.yr;
return write;
}
Read
是您要从中提取的 stream
的名称。您可以读取的流示例是 cin
。您需要替换此行:
l[i].Read(); //ERROR HERE
和
cin >> l[i];
在 operator>>
中,cin
对象现在称为 Read
。
您的 operator<<
的问题是它需要声明为 friend
,就像您对 operator>>
所做的那样。
此外,您正在写入 cout
而不是写入 write
。一旦您尝试写入任何其他流,这将不起作用。