Because it's not a friend of that function: the signature is different. Note const
modifier of the second parameter.
friend cannot access private variables?
题
class LongInt
{
friend ostream & operator <<(ostream & os, const LongInt & integer);
...
}
ostream & operator <<(ostream & os, LontInt & container)
{
os << container.number.size(); //error here
return os;
}
error: 'std::vector LongInt::number' is private vector number; ^
I don't understand why I can't access the variable, does it have to do something with the fact that the member variable is a vector?
解决方案
其他提示
I think you are missing a const
in the declaration before LongInt
, which makes the signatures different, and the compiler doesn't think it's the same function that you declared as friend.
不隶属于 StackOverflow