By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,501 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,501 IT Pros & Developers. It's quick & easy.

How to reference out of namespace friend

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.