473,554 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Friend in Class Can't Access Private Function

CMain Class is the base class that is initialized in main function. CA
Class is the base class that is initialized in CMain::CMain(). CMain Class
is always public while CA Class is always private. I have placed "friend
void CA::Run_A(void) " in CMain Class. CMain::Run() function attempts to
execute CA::Run_A(), but compiler shows an error saying that it is the
violation to access private function.
I don't understand why because friend should be able to access private
function. Please advise.

/* A.h */
class CMain;

class CA
{
public:
CA(CMain* pMain);
~CA();

private:
CA();
void Run_A(void);
CMain* m_pMain;
};

/* A.cpp */
#include "A.h"

CA::CA()
{

}

CA::CA(CMain* pMain)
{
m_pMain = pMain;
}

CA::~CA()
{

}

void CA::Run_A(void)
{

}

#include <stdio.h>
#include "A.h"

class CMain
{
public:
CMain();
~CMain();
void Run(void);
friend void CA::Run_A(void) ;

private:
CA* m_pA;
};

CMain::CMain()
{
m_pA = new CA(this);
}

CMain::~CMain()
{
delete m_pA;
}

void CMain::Run(void )
{
CA::Run_A();
}

void main(void)
{
CMain Main;
}
--
Bryan Parkoff
Jul 22 '05 #1
12 3231
On Mon, 12 Apr 2004 01:26:08 GMT, "Bryan Parkoff"
<br************ ******@nospam.c om> wrote:
CMain Class is the base class that is initialized in main function. CA
Class is the base class that is initialized in CMain::CMain(). CMain Class
is always public while CA Class is always private. I have placed "friend
void CA::Run_A(void) " in CMain Class. CMain::Run() function attempts to
execute CA::Run_A(), but compiler shows an error saying that it is the
violation to access private function.
I don't understand why because friend should be able to access private
function. Please advise.
You have two problems. Firstly, your CMain class is trying to "claim"
friendship of that function in CA. That's not how it works. The class with
the thing you need access to must /grant/ friendship to the class needing
the access. So you put something like
friend class CMain;
into CA's class definition (or you can limit it to specific functions, as
you've shown you have the general idea about.)

The other thing is that you're calling a member function as if it were a
static function. See below...

/* A.h */
class CMain;

class CA
{
public:
CA(CMain* pMain);
~CA();

private:
CA();
void Run_A(void);
CMain* m_pMain;
};

/* A.cpp */
#include "A.h"

CA::CA()
{

}

CA::CA(CMain * pMain)
{
m_pMain = pMain;
}

CA::~CA()
{

}

void CA::Run_A(void)
{

}

#include <stdio.h>
#include "A.h"

class CMain
{
public:
CMain();
~CMain();
void Run(void);
friend void CA::Run_A(void) ;

private:
CA* m_pA;
};

CMain::CMain ()
{
m_pA = new CA(this);
}

CMain::~CMain( )
{
delete m_pA;
}

void CMain::Run(void )
{
CA::Run_A();
Above, you can do something like:
m_pA -> Run_A();
(after your classes have made friends) but you can't call a member function
without an object to invoke it on.
-leor

void main(void)
{
CMain Main;
}


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
On Mon, 12 Apr 2004 01:47:58 GMT, Leor Zolman <le**@bdsoft.co m> wrote:
(after your classes have made friends) but you can't call a member function
without an object to invoke it on.


I'm getting tired. The two places whre I said "member function", what I
really meant was "non-static" member function.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3
Leor,

Thank you for the answer. If I place "friend class CMain" in CA class,
all functions and data members will be accessed by CMain class. I do not
allow to happen. What is another way to allow only one private function be
accessed as friend? I don't understand what you mean static or non static
function like outside of class. Please explain.

--
Bryan Parkoff
"Leor Zolman" <le**@bdsoft.co m> wrote in message
news:v2******** *************** *********@4ax.c om...
On Mon, 12 Apr 2004 01:26:08 GMT, "Bryan Parkoff"
<br************ ******@nospam.c om> wrote:
CMain Class is the base class that is initialized in main function. CAClass is the base class that is initialized in CMain::CMain(). CMain Classis always public while CA Class is always private. I have placed "friend
void CA::Run_A(void) " in CMain Class. CMain::Run() function attempts to
execute CA::Run_A(), but compiler shows an error saying that it is the
violation to access private function.
I don't understand why because friend should be able to access privatefunction. Please advise.
You have two problems. Firstly, your CMain class is trying to "claim"
friendship of that function in CA. That's not how it works. The class with
the thing you need access to must /grant/ friendship to the class needing
the access. So you put something like
friend class CMain;
into CA's class definition (or you can limit it to specific functions, as
you've shown you have the general idea about.)

The other thing is that you're calling a member function as if it were a
static function. See below...

/* A.h */
class CMain;

class CA
{
public:
CA(CMain* pMain);
~CA();

private:
CA();
void Run_A(void);
CMain* m_pMain;
};

/* A.cpp */
#include "A.h"

CA::CA()
{

}

CA::CA(CMain * pMain)
{
m_pMain = pMain;
}

CA::~CA()
{

}

void CA::Run_A(void)
{

}

#include <stdio.h>
#include "A.h"

class CMain
{
public:
CMain();
~CMain();
void Run(void);
friend void CA::Run_A(void) ;

private:
CA* m_pA;
};

CMain::CMain ()
{
m_pA = new CA(this);
}

CMain::~CMain( )
{
delete m_pA;
}

void CMain::Run(void )
{
CA::Run_A();


Above, you can do something like:
m_pA -> Run_A();
(after your classes have made friends) but you can't call a member

function without an object to invoke it on.
-leor

void main(void)
{
CMain Main;
}


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html

Jul 22 '05 #4
On Mon, 12 Apr 2004 02:26:01 GMT, "Bryan Parkoff"
<br************ ******@nospam.c om> wrote:
Leor,

Thank you for the answer. If I place "friend class CMain" in CA class,
all functions and data members will be accessed by CMain class. I do not
allow to happen. What is another way to allow only one private function be
accessed as friend?
In your CA class definition, put:
friend void CMain::Run(void );
to make just that one function of CMain a friend of CA. Since you'd done
this "backwards" in the first place, I just figured you were familiar with
the syntax.

Note that you'll have some ordering issues; I had to put the CMain class
definition up before the CA class definition so that when (in CA) you
declare CMain::Run to be a friend, the compiler recognizes Run as being a
member function of CMain.
I don't understand what you mean static or non static
function like outside of class. Please explain.


All your functions are non-static; that is, they don't use the word
"static" in their declarations. That means they operate upon an object of
their class, not "in a vacuum". Here's your CMain::Run implementation:

void CMain::Run(void )
{
// CA::Run_A();
m_pA->Run_A();
}

The commented-out line is the one you originally had. It says "run the
CA::Run_A() function". But that function can't run without a CA object to
invoke it on. That syntax would only work if CA was a base class of CMain
and had a Run_A function, or if CA::Run_A was a static member function.

This is basic stuff; if you have a C++ book handy, just look up "static
member function".

HTH,
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #5
> In your CA class definition, put:
friend void CMain::Run(void );
to make just that one function of CMain a friend of CA. Since you'd done
this "backwards" in the first place, I just figured you were familiar with
the syntax.

Note that you'll have some ordering issues; I had to put the CMain class
definition up before the CA class definition so that when (in CA) you
declare CMain::Run to be a friend, the compiler recognizes Run as being a
member function of CMain.

It is very close, but one problem exists. I already put "friend void
CMain::Run(void );" inside CA class. The error compiler reports, "C2027: use
of undefined type 'CMain' and see declaration of 'CMain' ".
I made sure to put definition there. I did put "#include "CMain.h"" and
"class CMain;" in A.h and A.cpp. Look at my code below. Please correct my
error.

/////////////////////////////////////////////////////////////////////
#include "CMain.h"

class CMain;

class CA
{
public:
friend void CMain::Run(void );
CA(CMain* pMain);
~CA();

private:
CA();
void Run_A(void);
CMain* m_pMain;
int test;
};
////////////////////////////////////////
////////////////////////////////////////
#include "A.h"
#include "CMain.h"

CA::CA()
{

}

CA::CA(CMain* pMain)
{
m_pMain = pMain;
test = 1024;
}

CA::~CA()
{

}

void CA::Run_A(void)
{

}

Bryan Parkoff
Jul 22 '05 #6
On Mon, 12 Apr 2004 03:32:21 GMT, "Bryan Parkoff"
<br************ ******@nospam.c om> wrote:
In your CA class definition, put:
friend void CMain::Run(void );
to make just that one function of CMain a friend of CA. Since you'd done
this "backwards" in the first place, I just figured you were familiar with
the syntax.

Note that you'll have some ordering issues; I had to put the CMain class
definition up before the CA class definition so that when (in CA) you
declare CMain::Run to be a friend, the compiler recognizes Run as being a
member function of CMain.

It is very close, but one problem exists. I already put "friend void
CMain::Run(voi d);" inside CA class. The error compiler reports, "C2027: use
of undefined type 'CMain' and see declaration of 'CMain' ".
I made sure to put definition there. I did put "#include "CMain.h"" and
"class CMain;" in A.h and A.cpp. Look at my code below. Please correct my
error.


You don't seem to have shown us CMain.h (and please, please always indicate
the filename of files you post, if there's more than one... best thing is a
comment at the top of the file with the file name, and white space
after...for /each/ file. And include /all/ the files! Thanks.)

The compiler is saying that at the point of that error, it still hasn't
seen the definition for CMain. Here's my test version from yesterday that
does compile; with any luck you'll figure out what you've done wrong by
comparing the ordering of things:
/* A.h */
class CA;

class CMain
{
public:
CMain();
~CMain();
void Run(void);

private:
CA* m_pA;
};

class CA
{
friend void CMain::Run(void );
public:
CA(CMain* pMain) {}
~CA() {}

private:
CA();
void Run_A(void) {}
CMain* m_pMain;
};
// ta.cpp:

#include <stdio.h>
#include "A.h"

CMain::CMain()
{
m_pA = new CA(this);
}

CMain::~CMain()
{
delete m_pA;
}

void CMain::Run(void )
{
// CA::Run_A();
m_pA->Run_A();
}

void main(void)
{
CMain Main;
}
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #7
"Leor Zolman" <le**@bdsoft.co m> wrote in message
news:t7******** *************** *********@4ax.c om...
<<snip>>
/* A.h */
class CA;

class CMain
{
public:
CMain();
~CMain();
void Run(void);

private:
CA* m_pA;
};

class CA
{
friend void CMain::Run(void );
public:
CA(CMain* pMain) {}
~CA() {}

private:
CA();
void Run_A(void) {}
CMain* m_pMain;
};
// ta.cpp:

#include <stdio.h>
#include "A.h"

CMain::CMain()
{
m_pA = new CA(this);
}

CMain::~CMain()
{
delete m_pA;
}

void CMain::Run(void )
{
// CA::Run_A();
m_pA->Run_A();
}

void main(void)
{
CMain Main;
}

Leor, when I compiled your "fixed" program I got the following error.

E:/DPR226/My Files/ta.cpp:23:'main ' must return 'int'

Otherwise, it seemed okay.
--
Gary
Jul 22 '05 #8
On Mon, 12 Apr 2004 10:18:47 -0400, "Gary Labowitz" <gl*******@comc ast.net>
wrote:
void main(void)
{
CMain Main;
}

Leor, when I compiled your "fixed" program I got the following error.

E:/DPR226/My Files/ta.cpp:23:'main ' must return 'int'

Otherwise, it seemed okay.


That's what I get for not compiling this with Comeau ;-)

I was just addressing the specific access and ordering issues the OP had
been struggling with, in this case, but of course main should return int.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #9
"Leor Zolman" <le**@bdsoft.co m> wrote in message
news:ub******** *************** *********@4ax.c om...
On Mon, 12 Apr 2004 10:18:47 -0400, "Gary Labowitz" <gl*******@comc ast.net> wrote:
void main(void)
{
CMain Main;
}

Leor, when I compiled your "fixed" program I got the following error.
E:/DPR226/My Files/ta.cpp:23:'main ' must return 'int'

Otherwise, it seemed okay.


That's what I get for not compiling this with Comeau ;-)


Isn't it psychologically interesting what amuses us when we are
unemployed?
--
Gary
Jul 22 '05 #10

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

Similar topics

12
426
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always private. I have placed "friend void CA::Run_A(void)" in CMain Class. CMain::Run() function attempts to execute CA::Run_A(), but compiler shows an error...
6
2325
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private data from the class. Here's the gist of the code: template <class T> struct NodeType { T data; NodeType<T> * link;
5
2419
by: Srini nandiraju | last post by:
Hi folks, I am trying to implement a friend class and the following is what I did. Please scroll down. /***************** CODE **********************************/ class BKP { private: int nVar;
15
6567
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local...
4
1058
by: Jeff Lindholm | last post by:
Is there a way to allow C++ friend level access in a VB.NET object. Simple C++ example............. class Foo { friend class Bar; private int a; ... };
2
2043
by: freegnu | last post by:
how to declare a friend function that can access two class it will look like the following class A { private: int i; public: A(){} ~A(){} friend void call(A &a, B &b);
1
3286
by: yancheng.cheok | last post by:
currently, i have a private function in cat named privateFun. i would like to have this function "private" to all except dog's action member function. by using the following approach, all the dog's members can access all the cat's private members. // cat.h //
3
2464
by: Rahul | last post by:
Hi Everyone, The following code works fine, class A { private: friend void sample(A& obj) { printf("in friend...\n"); printf("%d",obj.i);
2
2420
by: Immortal Nephi | last post by:
I design a class for general purpose. I do not allow a client to read or modify interface and implemention. I allow them to write a new non- member function outside of class' interface and implmention. The problem is that non-member function cannot access private data member. The friend keyword is not the solution. I am aware that friend...
1
7541
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7872
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6123
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5423
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5142
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3545
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3533
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2006
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
823
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.