LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2013, 04:35 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
[C++] How to access a private member of a class through a friend function of another namespace?


Code:
namespace aa
{
    void in ();
};

class x
{
public:
    friend void aa :: in ();

private:
    void pp ()
    {
    }
};

void aa :: in () 
{
    pp ();
}
results in :

Code:
In function ‘void aa::in()’:
error: ‘pp’ was not declared in this scope
Please point out the point that I am missing.
 
Old 02-12-2013, 05:00 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
I hope this helps: http://stackoverflow.com/questions/3...er-a-namespace
 
Old 02-12-2013, 05:02 AM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
I didn't create an object before calling that private member from the friend function, hence the error.
Quote:
x obj;
obj.pp ();
In that thread, the class there is in namespace (different issue).

Last edited by Aquarius_Girl; 02-12-2013 at 05:04 AM.
 
Old 02-12-2013, 05:09 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
so you need to call x::pp() instead of just pp() because pp is a member of x. Next, you cannot call a non-static member without instance, so you either need to make pp static or make an instance of x to be able to call pp

Last edited by pan64; 02-12-2013 at 05:10 AM. Reason: added noparse
 
Old 02-12-2013, 05:11 AM   #5
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
I know that and did already realize in my previous post.
It had just slipped out of my mind temporarily.

Last edited by Aquarius_Girl; 02-12-2013 at 05:13 AM.
 
Old 02-12-2013, 05:14 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
glad to help you
if you find a post useful just press YES (bottom right corner)
and also don't forget to mark SOLVED

Last edited by pan64; 02-12-2013 at 05:15 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why can't we access a protected member in a derived class by a base class's object? Aquarius_Girl Programming 5 02-04-2012 10:35 PM
Does making a class a 'friend' make the class' member classes 'friends'? Jusctsch Programming 7 11-17-2011 07:58 PM
problem in using member function of a class shamjs Programming 3 10-19-2011 06:14 AM
[SOLVED] How can a friend function access a public function from a base class? lesca Programming 1 11-17-2010 01:56 PM
friend class doesn't work when I put in namespace? Winter Knight Programming 10 12-13-2007 12:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration