假设我有这样的课程:
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
知道为什么会这样吗?