-2
我在这里有一个小问题,我无法处理。所以,我宣布我的班级先生是这样的:为什么我不能在重载“<<”运算符时访问在我的类中声明的成员?
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;
}
}
};
它说不能访问在类中声明的受保护成员。我在想什么?
'sir._elemente'看起来应该是'sir.elements',但othwerwise您不会错过任何东西,代码是好的。 – jrok
看起来你想让它成为一个非成员的朋友(因为有'friend'关键字存在),但是通过定义它内联,它实际上是作为一个成员函数实现的(这意味着不需要“friend”关键字)。编译器给你的_exact_错误信息是什么? – Chad
@Chad:'friend'关键字正是非成员函数的作用。 –