473,326 Members | 2,081 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,326 software developers and data experts.

Behavior of friend class in a namespace.

Consider the following piece of code:

// test.cpp
class B ;

namespace N
{
class A
{
friend class B ;
protected:
int i ;
} ;
}

class B
{
N::A a ;
public:
B() { a.i = 42 ; }
} ;
g++ 3.4.6 will compile this without complaint. g++ 4.1.0, however,
gives the error:
test.cpp: In constructor ‘B::B()’:
test.cpp:9: error: ‘int N::A::i’ is protected
test.cpp:17: error: within this context

If the friend declaration is changed to:
friend class ::B ;

then g++ 4.1.0 also accepts the code.

I am trying to determine which (if either) behavior is correct, but I'm
having trouble locating the relevant section of the standard. Any help
would be appreciated.

--
Alan Johnson
Mar 23 '07 #1
1 2177
Alan Johnson wrote:
Consider the following piece of code:

// test.cpp
class B ;

namespace N
{
class A
{
friend class B ;
protected:
int i ;
} ;
}

class B
{
N::A a ;
public:
B() { a.i = 42 ; }
} ;
g++ 3.4.6 will compile this without complaint. g++ 4.1.0, however,
gives the error:
test.cpp: In constructor ‘B::B()’:
test.cpp:9: error: ‘int N::A::i’ is protected
test.cpp:17: error: within this context

If the friend declaration is changed to:
friend class ::B ;

then g++ 4.1.0 also accepts the code.

I am trying to determine which (if either) behavior is correct, but I'm
having trouble locating the relevant section of the standard. Any help
would be appreciated.
I believe 4.1 is correct. The friend declaration in the unmodified code
declares N::B to be a friend.

Mar 24 '07 #2

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

Similar topics

4
by: Hans De Winter | last post by:
Why does my compiler complain with the message "`int*My::Test::_shape' is private" when I try to compile the following code? It seems it has something to do with the namespace since when I leave...
4
by: marco_segurini | last post by:
Hi, the following test program shows a solution to a problem I have had. Now, this test program is compiled and linked by VS2003 and g++ while Comeau-on-line-compiler fails with this messages:...
9
by: Xiangliang Meng | last post by:
The fragment code: #include <iostream> using namespace std; class Integer { int i; public: Integer() : i(0) {};
4
by: edgekaos | last post by:
This doesn't seem to work in VC.Net. cpp file that include something like this would have a ambiguous symbol problem with two MyFriend declarations. Any ideas? namespce mynamespace { class...
9
by: Ivan Mascovich | last post by:
Previous posts (and compilers) confirm that class X { friend void Y() ; } ; does not match namespace
2
by: Layton | last post by:
Hi, CPP gurus, How to use friend function cross the namespace? I have the following sample code with operator << overloaded, it's working. The problem the operator << function can't access...
3
by: Filimon Roukoutakis | last post by:
Dear all, I have the following concept name { space1 { class Friend { };
6
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend...
4
by: rn | last post by:
the code below compiles OK with gcc 4.0 but not with gcc.4.2 and visual studio 2005 (ver 8 ). to me it seems it should not compile. ----- class test1 { public:
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.