How to reference out of namespace friend

E

edgekaos

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
{
};
 
J

Jonathan Mcdougall

edgekaos said:
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-lite/how-to-post.html#faq-5.8

Jonathan
 
E

edgekaos

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.
 
J

Jonathan Mcdougall

edgekaos said:
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
 
J

Jonathan Mcdougall

Jonathan said:
Please, quote the message you are answering to.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top