473,659 Members | 2,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me 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(Ou ter::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 1730
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<typena me 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(Ou ter::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<typena me 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(Ou ter::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.prof esser 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<typena me 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(Ou ter::A<int>&)':
friend.cpp:1 6: error: 'int Outer::A<int>:: a' is protected
friend.cpp:2 4: 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
5962
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 get the following error: Error: Unresolved external 'operator <<(std::basic_ostream<char, std::char_traits<char> >&, const BinomialTree<int>&)' referenced from C:\DOCUMENTS AND SETTINGS\RYAN\DESKTOP\COMPUTER SCIENCE\CS
1
3133
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 friend classes as "friend ClassName", which is easy to remedy by replacing "friend" with "friend class". But this poses another problem: in the file metadata.h I encounter the following code: ....
6
2330
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 data from the class. Here's the gist of the code: template <class T> struct NodeType { T data; NodeType<T> * link;
7
1944
by: Mark P | last post by:
Is this legal and sensible? class Outer { friend struct Inner { ... }; ...
2
2503
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
1425
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 created. But, I can't get the friend class to work. For example (this is not actually my code): template <typename Key, typename Value> class MyMap; template <typename Key, typename Value> struct MyMapNode;
1
2120
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
1971
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; public:
1
1399
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 I donig wrong/ Thanks, Theodore //#define NO_TEMPLATE_Outter #ifndef NO_TEMPLATE_Outter // FAILS compiling when Outter NOT a TEMPLATE
0
8427
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.