In the code below I have the operator << work OK for classes A and B
but cannot make it work for class C. The error I am getting currently
points to this line:
friend std::ostream& operator <<(std::ostream&, const B::C&);
saying class C (in const B::C&) is inaccessible, although, as I see
it, this has been added to class B:
friend std::ostream& operator <<(std::ostream&, const C&);
Is there a way to make it work?
Thank you.
#include <ostream>
class A {
class B {
class C {
friend std::ostream& operator <<(std::ostream&, const C&);
};
friend std::ostream& operator <<(std::ostream&, const B&);
friend std::ostream& operator <<(std::ostream&, const C&);
};
friend std::ostream& operator <<(std::ostream&, const A&);
friend std::ostream& operator <<(std::ostream&, const B&);
friend std::ostream& operator <<(std::ostream&, const B::C&);
};
inline std::ostream& operator <<(std::ostream& os, const
A&) { return os; }
inline std::ostream& operator <<(std::ostream& os, const
A::B&) { return os; }
inline std::ostream& operator <<(std::ostream& os, const
A::B::C&) { return os; }
|
|
0
|
|
|
|
Reply
|
Paul
|
9/20/2010 1:12:43 PM |
|
On Sep 20, 4:12=A0pm, Paul <paul.v....@googlemail.com> wrote:
> In the code below I have the operator << work OK for classes A and B
> but cannot make it work for class C. The error I am getting currently
> points to this line:
>
> =A0 =A0 friend std::ostream& operator <<(std::ostream&, const B::C&);
>
> saying class C (in const B::C&) is inaccessible, although, as I see
> it, this has been added to class B:
>
> =A0 =A0 =A0 =A0 friend std::ostream& operator <<(std::ostream&, const C&)=
;
>
> Is there a way to make it work?
>
> Thank you.
>
> #include <ostream>
>
> class A {
> =A0 =A0 class B {
> =A0 =A0 =A0 =A0 class C {
> =A0 =A0 =A0 =A0 =A0 =A0 friend std::ostream& operator <<(std::ostream&, c=
onst C&);
> =A0 =A0 =A0 =A0 };
>
> =A0 =A0 =A0 =A0 friend std::ostream& operator <<(std::ostream&, const B&)=
;
> =A0 =A0 =A0 =A0 friend std::ostream& operator <<(std::ostream&, const C&)=
;
> =A0 =A0 };
>
> =A0 =A0 friend std::ostream& operator <<(std::ostream&, const A&);
> =A0 =A0 friend std::ostream& operator <<(std::ostream&, const B&);
> =A0 =A0 friend std::ostream& operator <<(std::ostream&, const B::C&);
>
> };
>
> inline std::ostream& operator <<(std::ostream& os, const
> A&) =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0{ return os; }
> inline std::ostream& operator <<(std::ostream& os, const
> A::B&) =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 { return os; }
> inline std::ostream& operator <<(std::ostream& os, const
> A::B::C&) =A0 =A0 =A0 =A0 =A0 =A0 =A0{ return os; }
This is about implicit private. Usually the very same people who use
it are also the ones who are most confused by it. Always specify
access explicitly, then you hopefully see yourself what you write.
|
|
0
|
|
|
|
Reply
|
ootiib (655)
|
9/20/2010 1:44:04 PM
|
|
On Sep 20, 2:44=A0pm, =D6=F6 Tiib <oot...@hot.ee> wrote:
>
> This is about implicit private. Usually the very same people who use
> it are also the ones who are most confused by it. Always specify
> access explicitly, then you hopefully see yourself what you write.- Hide =
quoted text -
I do appreciate that I did not explicitly use the word 'private' to
denote a private section in a class:
class A {
private:
// data member/functions
};
and do apologise if you found this confusing. The real class has more
members than in the example provided and contains both public and
private sections exlicitly introduced by public/private keywords. None
the less, with the keyword 'private' inserted, the sections in the
classes in the above example are private by design - and I was trying
to work it out how to use 'friend' declarations to make required
sections accessible to the 'operator <<' operators.
|
|
0
|
|
|
|
Reply
|
Paul
|
9/20/2010 2:26:46 PM
|
|
Paul wrote:
> On Sep 20, 2:44 pm, �� Tiib <oot...@hot.ee> wrote:
>>
>> This is about implicit private. Usually the very same people who
>> use
>> it are also the ones who are most confused by it. Always specify
>> access explicitly, then you hopefully see yourself what you
>> write.- Hide quoted text -
>
> I do appreciate that I did not explicitly use the word 'private' to
> denote a private section in a class:
>
> class A {
> private:
> // data member/functions
> };
>
> and do apologise if you found this confusing. The real class has
> more
> members than in the example provided and contains both public and
> private sections exlicitly introduced by public/private keywords.
> None
> the less, with the keyword 'private' inserted, the sections in the
> classes in the above example are private by design - and I was
> trying
> to work it out how to use 'friend' declarations to make required
> sections accessible to the 'operator <<' operators.
And in that case, A (and things declared in A) cannot access B::C
because C is private to B.
Bo Persson
|
|
0
|
|
|
|
Reply
|
Bo
|
9/20/2010 6:55:18 PM
|
|
On 20 sept, 17:26, Paul <paul.v....@googlemail.com> wrote:
> On Sep 20, 2:44=A0pm, =D6=F6 Tiib <oot...@hot.ee> wrote:
>
>
>
> > This is about implicit private. Usually the very same people who use
> > it are also the ones who are most confused by it. Always specify
> > access explicitly, then you hopefully see yourself what you write.- Hid=
e quoted text -
>
> I do appreciate that I did not explicitly use the word 'private' to
> denote a private section in a class:
>
> class A {
> private:
> =A0 =A0 // data member/functions
>
> };
>
> and do apologise if you found this confusing. The real class has more
> members than in the example provided and contains both public and
> private sections exlicitly introduced by public/private keywords. None
> the less, with the keyword 'private' inserted, the sections in the
> classes in the above example are private by design - and I was trying
> to work it out how to use 'friend' declarations to make required
> sections accessible to the 'operator <<' operators.
Yes but does not error message tell to you that A::B::C is private for
B and so not accessible for A?
The friend declaration does declare but does not introduce names into
enclosing namespace. Therefore friend declarations in A::B::C or in
A::B do not declare the friend function for A.
The class A::B::C must be accessible for A to declare a function using
it in A. Other option is that the function is declared in enclosing
namespace first and then any class may claim friendship with it (but
not A, because functions using A::B::C can not be declared before A is
defined).
|
|
0
|
|
|
|
Reply
|
ISO
|
9/20/2010 8:17:34 PM
|
|
|
4 Replies
146 Views
(page loaded in 0.19 seconds)
|