473,799 Members | 3,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_funct ion1() 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 2595
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_funct ion1() 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_funct ion1() 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_funct ion1() 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
5780
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I defined a specialization of print() for C<string, char> which is called correctly. Then I wanted...
12
3257
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...
7
2134
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
1282
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 functions currently exist in the class definition for object A. The thing is, there is another object (B) who's job it is to call these functions when the app starts up, defining all possibilities for object A's, then allow future object A's to look up...
1
2158
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 definition. If you insted have declared a primitive type or a class type then I would understand it completely. I assume that having a friend declaration in the private section make no sense. I assume that you always put friend declaration in the...
3
3762
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 thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
4
8001
by: zfareed | last post by:
#include <iostream> #include <fstream> using namespace std; template<class ItemType> class SortedList { private:
11
4761
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++ book (except for the "friend"). can someone explain why GCC is giving me problems here? for a += or similar operator, what does a proper declaration look like and what are its arguments for?
6
2612
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 inside template class definition (commented out at //1) then it compiles and runs fine, but if I declare and define the function separately (at //2). Is the following syntax supported by g++?
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10484
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
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...
1
10228
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
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
9072
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...
0
6805
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
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2938
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.