Scuba Steve
Scuba Steve

Reputation: 1648

Trouble accessing private members with friend function in c++ operator<< overload

So I'm trying to overload the << operator. From all the sources I can see, the syntax is correct, but eclipse doesn't like it.

I'm getting a couple errors: Polynomial::PrivateStruct* Polynomial::head is private

And: struct Polynomial::PrivateStruct is private.

I want to keep this struct private as to hide implementation details.

std::ostream& operator<<(std::ostream& outputStream, Polynomial& rhs)
    {
        Polynomial::PrivateStruct *p = rhs.head;
        //implementation details
        return outputStream;

    }

and the declaration:

friend std::ostream& operator<<(std::ostream& outputStream, const Polynomial& rhs);

Upvotes: 0

Views: 315

Answers (1)

jrok
jrok

Reputation: 55425

The declaration and the definition don't match - one takes a reference to const, the other to non-const. Match them and you're good to go.

Upvotes: 6

Related Questions