473,511 Members | 14,975 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to reference out of namespace friend

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 MyClass
{
friend class MyFriend;
};
};

class MyFriend
{
};

Nov 8 '05 #1
4 1808
edgekaos wrote:
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
Thats should be "namespace".
{
class MyClass
{
friend class MyFriend;
};
};
Remove that semicolumn.

class MyFriend
{
};


This

namespace mynamespace
{
class MyClass
{
friend class MyFriend;
};
}

class MyFriend
{
};

int main()
{
mynamespace::MyClass c;
MyFriend f;
}

compiles fine. Looks like the code you posted isn't the one causing the
problem.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Jonathan

Nov 8 '05 #2
Sorry I was lazy just made up the sample during the post. Here is the
correct version

namespace MyNameSpace
{
class MyClass
{
friend class MyFriend;
int member;
};
}

class MyFriend
{
MyFriend()
{
MyNameSpace::MyClass myClass;
myClass.member = 1;
}
};

name this as MyClass.cpp and compile cause:

MyClass.cpp(15): error C2248: 'MyNameSpace::MyClass::member' : cannot
access private member declared in class 'MyNameSpace::MyClass'

My guess is that I need to change this line:
friend class MyFriend;
to something along the lines of:
friend class ::MyFriend;
to tell compiler MyFriend is in the global namespace.

Nov 8 '05 #3
edgekaos wrote:
Sorry I was lazy just made up the sample during the post. Here is the
correct version
Please, quote the message you are answering to.
namespace MyNameSpace
{
class MyClass
{
friend class MyFriend;
This is equivalent to

friend class MyNameSpace::MyFriend;

which is clearly not what you want. You'll have to qualify it:

friend class ::MyFriend;
int member;
};
}

class MyFriend
{
MyFriend()
{
MyNameSpace::MyClass myClass;
myClass.member = 1;
}
};

name this as MyClass.cpp and compile cause:

MyClass.cpp(15): error C2248: 'MyNameSpace::MyClass::member' : cannot
access private member declared in class 'MyNameSpace::MyClass'

My guess is that I need to change this line:
friend class MyFriend;
to something along the lines of:
friend class ::MyFriend;
to tell compiler MyFriend is in the global namespace.


Yes.
Jonathan

Nov 8 '05 #4
Jonathan Mcdougall wrote:
edgekaos wrote:
Sorry I was lazy just made up the sample during the post. Here is the
correct version


Please, quote the message you are answering to.
namespace MyNameSpace
{
class MyClass
{
friend class MyFriend;


This is equivalent to

friend class MyNameSpace::MyFriend;

which is clearly not what you want. You'll have to qualify it:

friend class ::MyFriend;


But note that now because the class name is qualified, it is searched
only in that scope. If it is not found, the program is ill-formed.
You'll have to declare ::MyFriend before that point:

class MyFriend;

namespace MyNameSpace
{
class MyClass
{
friend class ::MyFriend;
int member;
};
}
Jonathan

Nov 8 '05 #5

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

Similar topics

4
3795
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:...
8
1656
by: JKop | last post by:
Having decided not to use macros at all in designing my reseatable references, and having looked through the list of overloadable operators and combinations of them, I've settled on the following...
3
4198
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
3
2421
by: Adriano | last post by:
Hello, when I try to print something, either DataGrid or from Crystal Report viever the folowing error message appears and cancels printing: Object reference not set to an instance of an...
5
2755
by: homsan toft | last post by:
Hi, I'm (still) trying to return a pair<const Key, T> from iterator dereference. So I defined a proxy class in the obvious way: template<class KeyT, class DataT> struct ref_proxy { typedef...
9
5394
by: Ivan Mascovich | last post by:
Previous posts (and compilers) confirm that class X { friend void Y() ; } ; does not match namespace
0
2092
by: Abhishek Padmanabh | last post by:
I have been trying out boost's serialization library for the past few days. And I have come across a problem serializing a class that has a reference member. The code is posted as below: ...
3
3482
by: Filimon Roukoutakis | last post by:
Dear all, I have the following concept name { space1 { class Friend { };
4
3568
by: mark4asp | last post by:
What is the best way to locate the namespace I need to put in a using statement? For example suppose I wanted to discover which namespace "Server" was in the context of: Exception ex =...
0
7371
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,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
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...
0
4743
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...
0
3230
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...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
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...

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.