Search code examples
c++friend

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?


Solution

  • Because it's not a friend of that function: the signature is different. Note const modifier of the second parameter.