473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

declaring class member functions as friend without full class definition

Hello,

I am attempting to change a existing large code base and I have this
situation:

A.hpp
class A {
....
friend void B::member_function1() const;
...
};
When I include B.hpp for the definition of B, I seem to fall in
cross-include situation where the include guards
don't work (or work bad...)

I tried forward declaring class B before A, but it seems the full definition
of B is required
to declare its member function as a friend to A.

Is there a solution to this?

regards,
Nov 11 '08 #1
2 2561
On 2008-11-11 16:19:07 -0500, "Hicham Mouline" <hi****@mouline.orgsaid:
Hello,

I am attempting to change a existing large code base and I have this
situation:

A.hpp
class A {
....
friend void B::member_function1() const;
...
};
When I include B.hpp for the definition of B, I seem to fall in
cross-include situation where the include guards
don't work (or work bad...)

I tried forward declaring class B before A, but it seems the full definition
of B is required
to declare its member function as a friend to A.

Is there a solution to this?
Sure. Put the full definition of B before the definition of A. Since
you haven't shown any other code, it's impossible to guess the causes
of whatever other problems you've been running into.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 11 '08 #2
"Hicham Mouline" <hi****@mouline.orgkirjutas:
Hello,

I am attempting to change a existing large code base and I have this
situation:

A.hpp
class A {
....
friend void B::member_function1() const;
...
};
When I include B.hpp for the definition of B, I seem to fall in
cross-include situation where the include guards
don't work (or work bad...)

I tried forward declaring class B before A, but it seems the full
definition of B is required
to declare its member function as a friend to A.
Correct.
>
Is there a solution to this?
If it is not possible to move B definition before A, then the simplest
solution is to define the whole (forward-declared) class B as a friend.
Another solution would be to remove the friend declaration and instead
define a member function in A exposing or twiddling the private bits, and
document that only B::member_function1() can call it.

The cleanest solution would be to redesign the code so that friendship is
not required. However, this may require enormous effort in case of an
"existing large code base".

Paavo

Nov 12 '08 #3

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

Similar topics

2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
12
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...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
3
by: deancoo | last post by:
I'm hoping this isn't too off topic, but I'm not sure if I've included some of my member functions in the right class. I needed to develop numerous functions to help define object A. These...
1
by: Tony Johansson | last post by:
Hello experts! Assume I have this class definition of class ListElem. I'm a bit unsure how to interpret when you put friend declaration in public, protected and private section of a class...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
4
by: zfareed | last post by:
#include <iostream> #include <fstream> using namespace std; template<class ItemType> class SortedList { private:
11
by: Jim Michaels | last post by:
friend fraction& operator+=(const fraction& rhs); fraction.h(64) Error: error: 'fraction& operator+=(const fraction&)' must take exactly two arguments I practically pulled this out of a C++...
6
by: Dan Smithers | last post by:
I want to write my own class derived from the ostream class. I have been getting errors with my templates: First, I get an error writing a nested template. If I leave the function definition...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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...
0
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,...

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.