473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Nested friend class in nested template problem

Hi all,

the following code does not compile in gcc 4.0.2:
3 class Outer
4 {
5 class B;
6
7 template<typename T>
8 class A
9 {
10 friend class Outer::B;
11
12 public:
13 A(T t): a(t) { };
14 A() { };
15 protected:
16 T a;
17 };
18
19 class B : public A<int>
20 {
21 public:
22 B(A<int& a)
23 {
24 b = a.a;
25 }
26
27 int b;
28 };
29 };

The error I get is:

friend.cpp: In constructor 'Outer::B::B(Outer::A<int>&)':
friend.cpp:16: error: 'int Outer::A<int>::a' is protected
friend.cpp:24: error: within this context

Note that when the Outer class is stripped, everything compiles just
fine. What is the problem here?
Dec 7 '07 #1
3 1713
to*************@gmail.com wrote:
Hi all,

the following code does not compile in gcc 4.0.2:
3 class Outer
4 {
5 class B;
6
7 template<typename T>
8 class A
9 {
10 friend class Outer::B;
11
12 public:
13 A(T t): a(t) { };
14 A() { };
15 protected:
16 T a;
17 };
18
19 class B : public A<int>
20 {
21 public:
22 B(A<int& a)
23 {
24 b = a.a;
25 }
26
27 int b;
28 };
29 };
Please don't post line numbers like you have here. It makes it
quite difficult to quickly copy-and-paste your code and compile it.
You can always use *comments* to identify the lines to which the
compiler is referring in its error messages.

Thanks.
>
The error I get is:

friend.cpp: In constructor 'Outer::B::B(Outer::A<int>&)':
friend.cpp:16: error: 'int Outer::A<int>::a' is protected
friend.cpp:24: error: within this context

Note that when the Outer class is stripped, everything compiles just
fine. What is the problem here?
A buggy compiler?

Your code compiles OK in Comeau online (4.3.9) or VC++ 2005 SP1.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 7 '07 #2
On Dec 7, 10:40 pm, tonvandenheu...@gmail.com wrote:
Hi all,

the following code does not compile in gcc 4.0.2:

3 class Outer
4 {
5 class B;
6
7 template<typename T>
8 class A
9 {
10 friend class Outer::B;
11
12 public:
13 A(T t): a(t) { };
14 A() { };
15 protected:
16 T a;
17 };
18
19 class B : public A<int>
20 {
21 public:
22 B(A<int& a)
23 {
24 b = a.a;
25 }
26
27 int b;
28 };
29 };

The error I get is:

friend.cpp: In constructor 'Outer::B::B(Outer::A<int>&)':
friend.cpp:16: error: 'int Outer::A<int>::a' is protected
friend.cpp:24: error: within this context

Note that when the Outer class is stripped, everything compiles just
fine. What is the problem here?
Note the error message:'int Outer::A<int>::a' is protected.

B is inherited form A as public inheritance,
So B can only call A's public and private members.
Dec 7 '07 #3
want.to.be.professer wrote:
On Dec 7, 10:40 pm, tonvandenheu...@gmail.com wrote:
>Hi all,

the following code does not compile in gcc 4.0.2:

3 class Outer
4 {
5 class B;
6
7 template<typename T>
8 class A
9 {
10 friend class Outer::B;
11
12 public:
13 A(T t): a(t) { };
14 A() { };
15 protected:
16 T a;
17 };
18
19 class B : public A<int>
20 {
21 public:
22 B(A<int& a)
23 {
24 b = a.a;
25 }
26
27 int b;
28 };
29 };

The error I get is:

friend.cpp: In constructor 'Outer::B::B(Outer::A<int>&)':
friend.cpp:16: error: 'int Outer::A<int>::a' is protected
friend.cpp:24: error: within this context

Note that when the Outer class is stripped, everything compiles just
fine. What is the problem here?

Note the error message:'int Outer::A<int>::a' is protected.

B is inherited form A as public inheritance,
So B can only call A's public and private members.
Actually your post made me think a bit more about this, and perhaps
I was too fast to judge GCC as buggy...

Usually the rule is, the derived class is not alloed to access
*another instance's protected members*, but only its own or of
another instace of _its own type_. IOW, you should get an error
in 'foo', but not in 'bar':

class A {
protected:
int a;
};

class B : A {
void foo(A& a) {
a.a; /// error!
}

void bar() {
this->a; // OK
}
};

However, in this particular case, since A<Tmakes 'B' its
friend, all the rules about protected access are overridden by
the rules of friendship, which allows access to _all_ members
regardless of their declared access specifiers.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 7 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: keit6736 | last post by:
Hi, I'm using the Borland compiler and I've created two templated classes in which I've overloaded the ostream << operator. However, when I try and use the operator on objects of either class I...
1
by: Alex Borghgraef | last post by:
Hi all, I'm trying to get the Limp (Large Image Manipulation Program) library to compile using gcc 3.2, and I've encountered some problems. One is that the library systematically refers to...
6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
7
by: Mark P | last post by:
Is this legal and sensible? class Outer { friend struct Inner { ... }; ...
2
by: xuatla | last post by:
The following is just a sample code to demostrate my question: ----------- template <typename T> class C { public: friend void f1(double i=2) { std::cout << i; } ; };
1
by: Joe Carner via .NET 247 | last post by:
First time posting, thanks in advance for any help you can give me. Basically I am trying to a class that i want to be able to access the private data members of another class, both of which i...
1
by: Tomas Sieger | last post by:
Hi all, I'm in doubt with the following code: class Base { public: class Nested {}; }; class Derived:public Base { public: class Nested {
8
by: flamexx7 | last post by:
Can anybody tell me what is wrong with declaration of pointer p ? template<class Tclass Stack { struct Link { T* data; Link* next; Link(T* dat, Link* nxt) : data(dat), next(nxt) {} }* head;...
1
by: Theodore B | last post by:
Hi, The Outter class FAILS COMPILING when I declare it as a template class. If I remove the template declaration from the Outter class (the "else" preprocessor) it compiles Can you tell me what am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.