473,395 Members | 1,948 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,395 software developers and data experts.

#include cycles disallow 'friend' function?

I am having difficulty defining a friend function because of a #include
cycle:

============ obj.H ================
#ifndef _obj_
#define _obj_

#include "cont.H"

class cont;

class obj {
friend void cont::foo(obj& o);
private:
void called_by_foo();
};

#endif

============ cont.H ================
#ifndef _cont_
#define _cont_

#include "obj.H"

#include <vector>

class cont {
public:
void foo(obj& o);
private:
std::vector<objv;
};

#endif

============ cont.C ================
#include"cont.H"
gcc -o cont.o cont.C
In file included from cont.H:4,
from cont.C:1:
obj.H:9: error: member `void cont::foo(obj&)' declared as friend before
type `cont' defined
===============

How can I fix this such that cont can contain obj, but obj can have a
member function of cont as a friend function?

Thanks,
Joseph

Nov 21 '06 #1
6 2035

Joseph Turian wrote:
I am having difficulty defining a friend function because of a #include
cycle:

============ obj.H ================
#ifndef _obj_
#define _obj_

#include "cont.H"

class cont;

class obj {
friend void cont::foo(obj& o);
private:
void called_by_foo();
};

#endif

============ cont.H ================
#ifndef _cont_
#define _cont_

#include "obj.H"

#include <vector>

class cont {
public:
void foo(obj& o);
private:
std::vector<objv;
};

#endif

============ cont.C ================
#include"cont.H"
gcc -o cont.o cont.C
In file included from cont.H:4,
from cont.C:1:
obj.H:9: error: member `void cont::foo(obj&)' declared as friend before
type `cont' defined
===============

How can I fix this such that cont can contain obj, but obj can have a
member function of cont as a friend function?

Thanks,
Joseph
I think you need the forward declaration of obj in cont.h

Nov 21 '06 #2
Joseph Turian wrote:
I am having difficulty defining a friend function because of a
#include cycle:

============ obj.H ================
#ifndef _obj_
#define _obj_

#include "cont.H"

class cont;
There is no sense forward-declaring a class if you include the
file that contains its full definition. However, this does not
seem to work for you. So, drop the inclusion altogether, then.
>
class obj {
friend void cont::foo(obj& o);
private:
void called_by_foo();
};

#endif
[..]
obj.H:9: error: member `void cont::foo(obj&)' declared as friend
before type `cont' defined

How can I fix this such that cont can contain obj, but obj can have a
member function of cont as a friend function?
You can't. Declare the entire class 'cont' a friend of 'obj'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 21 '06 #3
am******@gmail.com wrote:
[..]
I think you need the forward declaration of obj in cont.h
Which will do what, exactly?
Nov 21 '06 #4

Victor Bazarov wrote:
am******@gmail.com wrote:
[..]
I think you need the forward declaration of obj in cont.h

Which will do what, exactly?
Unless there is another bug in VC++, but I did a quick addition of the
forward declaration and that compiled.
I think he had nested includes of header files which was causing one of
the problems...

Nov 21 '06 #5
Joseph Turian wrote:
I am having difficulty defining a friend function because of a #include
cycle:
Cyclic includes are never correct. Think it through: your source code
pulls in cont.h. cont.h first defines the macro _cont_, then pulls in
obj.h. obj.h first defines the macro _obj_, then pulls in cont.h. When
compiling cont.h, the compiler sees that _cont_ is already defined, so
it skips to the end of the ifndef block.

The problem has nothing to do with friend functions. You're not getting
the definition of cont at the point where you're using it in obj.h.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Nov 21 '06 #6
"Joseph Turian" <tu****@gmail.comwrote in message
news:11*********************@j44g2000cwa.googlegro ups.com
I am having difficulty defining a friend function because of a
#include cycle:

============ obj.H ================
#ifndef _obj_
#define _obj_

#include "cont.H"

class cont;

class obj {
friend void cont::foo(obj& o);
private:
void called_by_foo();
};

#endif

============ cont.H ================
#ifndef _cont_
#define _cont_

#include "obj.H"

#include <vector>

class cont {
public:
void foo(obj& o);
private:
std::vector<objv;
};

#endif

============ cont.C ================
#include"cont.H"
>gcc -o cont.o cont.C
In file included from cont.H:4,
from cont.C:1:
obj.H:9: error: member `void cont::foo(obj&)' declared as friend
before type `cont' defined
===============

How can I fix this such that cont can contain obj, but obj can have a
member function of cont as a friend function?

Thanks,
Joseph
If you wanted to also make a friend member declaration in cont, then you
would have a problem. However, since you only want to do it in obj, your
problem is probably soluble.

First note that you should NEVER use mutual inclusion because it it not
possible to achieve what you think you are achieving. When obj.h #includes
cont.h, that means you want cont.h read first. When cont.h #includes obj.h,
that means you want obj.h read first. Clearly, it is not possible for both
these things to be true; in any translation unit one file will be read first
and the other file will be read second. There is no way around that.

To make the member friend declaration, cont.h must be read first. Within
cont, you will need a forward declaration of obj.

By the way, you should not use leading underscores in your macro names lest
you hav a name clash with the implementation.

Your files should be:

///////////////////////////////////////////////////////////////////////
============ cont.H ================
#ifndef cont_
#define cont_
#include <vector>

class obj;

class cont {
public:
void foo(obj& o);
private:
std::vector<objv;
};

#endif
============ obj.H ================
#ifndef _obj_
#define _obj_

#include "cont.H"

class obj {
friend void cont::foo(obj& o);
private:
void called_by_foo();
};

#endif


============ cont.C ================
#include "obj.H"

/////////////////////////////////////////////////////////////////////////////////////

Not that the above scheme means that

std::vector<objv;

is being declared using an incomplete type. This is not guaranteed to
succeed, but does succeed on most implementations, including VC++ and
Comeau. If it doesn't succeed with your compiler, then you will need to
reverse the order of inclusion (read obj.h first and cont.h second) and make
the whole cont class a friend of obj, as per Victor's suggestion.
--
John Carson
Nov 21 '06 #7

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

Similar topics

2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
28
by: Ramesh | last post by:
Hi, I am currently maintaining a legacy code with a very very large code base. I am facing problems with C/C++ files having a lot of un-necessary #includes. On an average every C/C++ file has...
15
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...
3
by: xxxxyz | last post by:
Hi, I have an array and I want to make many "for" cycles as follows: int a=new int; ....//init a for (i1=0;i1<a.Length;i1++) for (i2=0;i2<a.Length-1;i2++) for (i3=0;i3<a.Length-2;i3++)
4
by: Martin Blais | last post by:
Hi I'm a tad confused over a problem involving cycles between packages. Assume the following sets of files:: driver.py a/__init__.py a/alice.py
1
by: gw7rib | last post by:
Suppose I have a function, whose protoype is somewhat complicated, for example, with some #defines and typedefs, it looks like this: INT_PTR CALLBACK PropDlgProc(HWND hDlg, UINT message, WPARAM...
5
by: windmill2008 | last post by:
I am trying to write two classes which can access the same base class as shown in the following code. My problem is when I include these two classes in the main.cpp, errors pop up saying that the...
6
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend...
21
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
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.