我在这里有一个小问题,我无法处理。所以,我宣布我的班级先生是这样的:
typedef unsigned int Uint;
typedef vector<Uint> TVint;
typedef vector<Uint>::const_iterator TIterator;
class Sir
{
protected:
Uint _count;
TVint _elements;
public:
...
friend std::ostream& operator << (std::ostream &out, const Sir &sir)
{
for(size_t i = 0; i < sir._count; i++)
{
out<<"[" << i << "]" << " " << sir._elemente[i]<<endl;
}
}
};
它表示无法访问类中声明的受保护成员。我错过了什么?
答案 0 :(得分:0)
有一个拼写错误,您应该在重载的_elements
而不是<<
中使用_elemente
。你应该从那个方法中返回一些东西:
friend std::ostream& operator << (std::ostream &out, const Sir &sir)
{
for(size_t i = 0; i < sir._count; i++)
{
out<<"[" << i << "]" << " " << sir._elements[i] <<endl;
//^^^^^^^^^^^^^^^^
}
return out;
//^^^^^^^^^^^
}