【发布时间】:2011-10-27 19:59:51
【问题描述】:
假设我有这样的课程:
class Ingredient
{
public:
friend istream& operator>>(istream& in, Ingredient& target);
friend ostream& operator<<(ostream& out, Ingredient& data);
private:
Measure myMeas;
MyString myIng;
};
在这个重载的朋友函数中,我试图设置myIng的值
istream& operator>>(istream& in, Ingredient& target)
{
myIng = MyString("hello");
}
在我看来,这应该可行,因为我在朋友函数中设置类成分的私有数据成员的值,并且朋友函数应该有权访问所有私有数据成员,对吗?
但我收到此错误:‘myIng’ was not declared in this scope
知道为什么会这样吗?
【问题讨论】:
标签: c++ friend-function