473,771 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why can't member function be friend?

I want to show you an example how two classes can have relationship
instead of an inheritance. The inheritance is not an option if you
want to define two classes.
The relationship between two classes can be friends when variables
can be modified in both classes. As far as we discussed in an earlier
post, but I try to make this code much clearer.
The C++ Compiler generates an error if you try member function to be
friend because member function has not defined yet.
Only one member function can modify variable in another class. It
prevents you to add more member functions to modify variables in
another class. You may not want to use "friend class" as your best
option.
Do you have a way to fix an error?

Here is my example code here.

#ifndef CLASS_A_H
#define CLASS_A_H

class B;

class A
{
public:
A();
~A();
void Modify_A(B &b);
friend void B::Modify_B(A &); // error C2027: use of undefined type
'B'

private:
// friend class B;
int m_A1;
int m_A2;
};

#endif // CLASS_A_H
#ifndef CLASS_B_H
#define CLASS_B_H

class A;

class B
{
public:
B();
~B();
void Modify_B(A &a);
friend void A::Modify_A(B &); // error C2027: use of undefined type
'A'

private:
// friend class A;
int m_B1;
int m_B2;
};

#endif // CLASS_B_H
// CLASS_A.CPP
#include "Class_A.h"
#include "Class_B.h"

A::A() : m_A1(0), m_A2(0)
{
}

A::~A()
{
}

void A::Modify_A(B &b)
{
b.m_B1 += 3;
b.m_B2 += 4;
}
// CLASS_B.CPP
#include "Class_B.h"
#include "Class_A.h"

B::B() : m_B1(0), m_B2(0)
{
}

B::~B()
{
}

void B::Modify_B(A &a)
{
a.m_A1 += 1;
a.m_A2 += 2;
}
// MAIN.CPP

#include "Class_A.h"
#include "Class_B.h"

int main(void)
{
A a;
B b;

a.Modify_A(b);
b.Modify_B(a);

return 0;
}

Nephi
Sep 4 '08 #1
1 2529
Im************@ hotmail.com wrote:
I want to show you an example how two classes can have relationship
instead of an inheritance. The inheritance is not an option if you
want to define two classes.
The relationship between two classes can be friends when variables
can be modified in both classes. As far as we discussed in an earlier
post, but I try to make this code much clearer.
The C++ Compiler generates an error if you try member function to be
friend because member function has not defined yet.
Yes. You sound surprised.
Only one member function can modify variable in another class. It
prevents you to add more member functions to modify variables in
another class. You may not want to use "friend class" as your best
option.
Do you have a way to fix an error?
You need a third class, a proxy, in which you will declare both A and B
as friends, and which you will define before either of those. Once it
is defined, you can make two functions in it to access private members
in A and in B, respectively, and make those members friends:

class A;
class B;
class AB_buddy {
static void access_As_priva tes_for_B(B& b, A& a);
static void access_Bs_priva tes_for_A(A& a, B& b);
};

class A {
friend AB_buddy::acces s_As_privates_f or_B(B& b, A& a);
....
};

....
[...]
int main(void)
Use of 'void' to indicate no arguments is so C. Perhaps you can take up
the habit of putting *nothing* where *nothing* is expected:

int main()
{
A a;
B b;

a.Modify_A(b);
b.Modify_B(a);

return 0;
}

Nephi
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 4 '08 #2

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

Similar topics

8
17879
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. Why can't the = (assignment) operator be overloaded as a friend function ? I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it said the following :
12
3256
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 saying that it is the violation to access private function. I don't understand why because friend...
2
1668
by: Gergely Korodi | last post by:
Hello, I have two classes, say A and B, defined in header files "A.hh" and "B.hh," respectively. The definition of class B looks like #ifndef _B_HH_ #define _B_HH_ #include "A.hh" // other includes here
4
2446
by: Justin Miller | last post by:
Ok, I tried to make that subject as descriptive as possible. What I'm trying to do: I'm attempting to use policies to create a generic memento (design pattern) template. My Memento template so far is pretty simple: using an idea of Andrei Alexandrescu (at least that's where I first read it) to create a lightweight way of overloading member functions since member function specialization is not possible.
6
4116
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend istream &operator>>(istream &in, Array &a) { for(int i=0; i<a.size; i++) // do something
6
1824
by: ghager | last post by:
Hi all, I must be blind or stupid. Please consider the following code: ---- .... template <class T> class P; template <class T> P<T> operator*(T,const P<T>&); template <class T>
7
2800
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an object of that class. And is that also the reason why if you use class member functions for operators << and >you have to write: someclass << cout; someclass >cin; ?
1
3302
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 //
7
3767
by: PengYu.UT | last post by:
Hi, I want to write a test function to test a class. The class has a private member function that need to be tested. Although I could modify the member function as protected or public, I do not want to modify the class definition. I'm wondering what is the standard way to test private member functions in a library. Thanks, Peng
0
10102
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9910
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.