473,666 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confusion over including header files...Can you give me a quick hand?

Ok, this may sound confusing....bu t it's really simple. If you're confused,
just look at my example code and it'll make sense.

Here's my situation. I have 2 classes....A and B.

Class A has a member variable of type B.
Class B has a member function which does calculations which are dependent
upon values of members of the Class A that owns it.

When in that function of class B, I don't know how I can gain access to it's
parent Class A's member variables, so my solution to the problem is to take
as a parameter to that function, a class A datatype. So, it's like this:

class A
{
public:
int x;
B myB;
:
:
};

class B
{
public:
int y;
void theFunction(A parentA);
:
:
};

void B::theFunction( A parentA){
y = parentA.x * 10;
}

I would call it like this:
A myA;
myA.myB(myA);

Now, the problem is, both A.h and B.h have to include eachother. When they
do, I get the following compilation errors:
A.h(10): error C2143: syntax error : missing ';' before 'B'
A.h(10): error C2501: 'A::B' : missing storage-class or type specifiers

Line 10 is the line that I declare B myB is A.h.

If I comment out theFunction in B and I comment out the include of A.h,
it'll compile no problem. But that obviously isn't the solution as B can't
use A.

So, I need one of 2 solutions. Either (and most preferably) I somehow gain
access to the parent object A's member variables, without having to pass A
as a parameter to B's function....or, I keep it like it is, but somehow get
it to compile.

Can anyone inform me how either of the above can be accomplished?

Oh...and I know I could just pass myA.x, but don't want to do that as the
real function is called often and is actually dependent upon many member
variables of A.

Thanks a lot for any help!
Jul 19 '05 #1
7 1799
"Eternally" <m@r.com> wrote...
Ok, this may sound confusing....bu t it's really simple. If you're confused, just look at my example code and it'll make sense.

Here's my situation. I have 2 classes....A and B.

Class A has a member variable of type B.
Class B has a member function which does calculations which are dependent
upon values of members of the Class A that owns it.

When in that function of class B, I don't know how I can gain access to it's parent Class A's member variables, so my solution to the problem is to take as a parameter to that function, a class A datatype.
That's one way to do it...

An alternative way would be to pass the 'A*' during construction
of the member B, and then B will always know where its parent is.
So, it's like this:

class A
{
public:
int x;
B myB;
:
:
};

class B
{
public:
int y;
void theFunction(A parentA);
I wouldn't pass by value, of course. It is better to pass by
reference, for example. All you need to declare is that 'A' is
a class, really. This should help:

void theFunction(cla ss A parentA);

and you don't need to include the declaration of A.
:
:
};

void B::theFunction( A parentA){
y = parentA.x * 10;
}

I would call it like this:
A myA;
myA.myB(myA);

Now, the problem is, both A.h and B.h have to include eachother. When they do, I get the following compilation errors:
A.h(10): error C2143: syntax error : missing ';' before 'B'
A.h(10): error C2501: 'A::B' : missing storage-class or type specifiers

Line 10 is the line that I declare B myB is A.h.

If I comment out theFunction in B and I comment out the include of A.h,
it'll compile no problem. But that obviously isn't the solution as B can't use A.

So, I need one of 2 solutions. Either (and most preferably) I somehow gain access to the parent object A's member variables, without having to pass A
as a parameter to B's function....or, I keep it like it is, but somehow get it to compile.
See above. The alternative solution I was talking about is to
declare 'B's constructor as accepting A&:

class A; // forward declataion
class B {
A& parent;
public:
B(A& p) : parent(p);
};

....
class A {
B myB;
public:
A() : myB(*this) {}
...
};

The compiler may not line the use of 'this' in the initialiser list,
but you can ignore the warning.
Can anyone inform me how either of the above can be accomplished?

Oh...and I know I could just pass myA.x, but don't want to do that as the
real function is called often and is actually dependent upon many member
variables of A.


Shouldn't it be a member of A, then?

Victor
Jul 19 '05 #2

"Victor Bazarov" <v.********@att Abi.com> wrote in message
news:vg******** ****@corp.super news.com...
"Eternally" <m@r.com> wrote...
Ok, this may sound confusing....bu t it's really simple. If you're

confused,
just look at my example code and it'll make sense.

Here's my situation. I have 2 classes....A and B.

Class A has a member variable of type B.
Class B has a member function which does calculations which are dependent upon values of members of the Class A that owns it.

When in that function of class B, I don't know how I can gain access to

it's
parent Class A's member variables, so my solution to the problem is to

take
as a parameter to that function, a class A datatype.


That's one way to do it...

An alternative way would be to pass the 'A*' during construction
of the member B, and then B will always know where its parent is.
So, it's like this:

class A
{
public:
int x;
B myB;
:
:
};

class B
{
public:
int y;
void theFunction(A parentA);


I wouldn't pass by value, of course. It is better to pass by
reference, for example. All you need to declare is that 'A' is
a class, really. This should help:

void theFunction(cla ss A parentA);

and you don't need to include the declaration of A.
:
:
};

void B::theFunction( A parentA){
y = parentA.x * 10;
}

I would call it like this:
A myA;
myA.myB(myA);

Now, the problem is, both A.h and B.h have to include eachother. When

they
do, I get the following compilation errors:
A.h(10): error C2143: syntax error : missing ';' before 'B'
A.h(10): error C2501: 'A::B' : missing storage-class or type specifiers

Line 10 is the line that I declare B myB is A.h.

If I comment out theFunction in B and I comment out the include of A.h,
it'll compile no problem. But that obviously isn't the solution as B

can't
use A.

So, I need one of 2 solutions. Either (and most preferably) I somehow

gain
access to the parent object A's member variables, without having to pass A as a parameter to B's function....or, I keep it like it is, but somehow

get
it to compile.


See above. The alternative solution I was talking about is to
declare 'B's constructor as accepting A&:

class A; // forward declataion
class B {
A& parent;
public:
B(A& p) : parent(p);
};

...
class A {
B myB;
public:
A() : myB(*this) {}
...
};

The compiler may not line the use of 'this' in the initialiser list,
but you can ignore the warning.
Can anyone inform me how either of the above can be accomplished?

Oh...and I know I could just pass myA.x, but don't want to do that as the real function is called often and is actually dependent upon many member
variables of A.


Shouldn't it be a member of A, then?

Victor


Hi,

That's a nice solution, and I'll probably use it, but the problem still is
that even if I comment out theFunction in B and all references to A, as long
as B is including A.h, those compiler errors still remain. If I comment out
#include "A.h", then it compiles without errors....but if I put it in the
errors come back.

If A.h includes B.h and B.h includes A.h, then those errors will be there.

Thanks for the help!
Jul 19 '05 #3
"Eternally" <m@r.com> wrote...
[...]
That's a nice solution, and I'll probably use it, but the problem still is
that even if I comment out theFunction in B and all references to A, as long as B is including A.h, those compiler errors still remain. If I comment out #include "A.h", then it compiles without errors....but if I put it in the
errors come back.
"Doctor, if I do this, it hurts. What should I do?"
"Don't do that."
If A.h includes B.h and B.h includes A.h, then those errors will be there.


Of course. The solution is not to have those circular includes.

Victor
Jul 19 '05 #4

"Eternally" <m@r.com> wrote in message
news:k7******** ***********@twi ster.nyroc.rr.c om...
Ok, this may sound confusing....bu t it's really simple. If you're confused, just look at my example code and it'll make sense.

Here's my situation. I have 2 classes....A and B.

Class A has a member variable of type B.
Class B has a member function which does calculations which are dependent
upon values of members of the Class A that owns it.

When in that function of class B, I don't know how I can gain access to it's parent Class A's member variables, so my solution to the problem is to take as a parameter to that function, a class A datatype. So, it's like this:

class A
{
public:
int x;
B myB;
:
:
};

class B
{
public:
int y;
void theFunction(A parentA);
:
:
};

void B::theFunction( A parentA){
y = parentA.x * 10;
}

I would call it like this:
A myA;
myA.myB(myA);

Now, the problem is, both A.h and B.h have to include eachother. When they do, I get the following compilation errors:
A.h(10): error C2143: syntax error : missing ';' before 'B'
A.h(10): error C2501: 'A::B' : missing storage-class or type specifiers

Line 10 is the line that I declare B myB is A.h.

If I comment out theFunction in B and I comment out the include of A.h,
it'll compile no problem. But that obviously isn't the solution as B can't use A.

So, I need one of 2 solutions. Either (and most preferably) I somehow gain access to the parent object A's member variables, without having to pass A
as a parameter to B's function....or, I keep it like it is, but somehow get it to compile.

Can anyone inform me how either of the above can be accomplished?

Oh...and I know I could just pass myA.x, but don't want to do that as the
real function is called often and is actually dependent upon many member
variables of A.

Thanks a lot for any help!


Something I have done in the face of circular dependencies is as illustrated
below. Im not especialy proud of it but it worked. If anyone has comments on
the technique Id be glad to hear them.

---- A.h ----

#ifndef A_H
#define A_H

class B; /* #include "B.h" */

class A
{ private:
B myB;
....
};
.....

/**
* By this point itll be possible to define the
* B class since A has already been defined
*/
#include "B.h"

#endif

---- B.h ----

#ifndef B_H
#define B_H

#include "A.h"

class B
{ public:
void theFunction(A*) ;
....
};
.....

#endif
----
Jul 19 '05 #5

"Ellarco" <no****@eircom. net> wrote in message
news:1p******** ***********@new s.indigo.ie...

"Eternally" <m@r.com> wrote in message
news:k7******** ***********@twi ster.nyroc.rr.c om...
Ok, this may sound confusing....bu t it's really simple. If you're
confused,
just look at my example code and it'll make sense.

Here's my situation. I have 2 classes....A and B.

Class A has a member variable of type B.
Class B has a member function which does calculations
which are dependent upon values of members of the Class A that owns it.

When in that function of class B, I don't know how I can gain access to it's
parent Class A's member variables, so my solution to the
problem is to take
as a parameter to that function, a class A datatype.
So, it's like this:
class A
{
public:
int x;
B myB;
:
:
};

class B
{
public:
int y;
void theFunction(A parentA);
:
:
};

void B::theFunction( A parentA){
y = parentA.x * 10;
}

I would call it like this:
A myA;
myA.myB(myA);

Now, the problem is, both A.h and B.h have to include eachother. When they
do, I get the following compilation errors:
A.h(10): error C2143: syntax error : missing ';' before
'B' A.h(10): error C2501: 'A::B' : missing storage-class or type specifiers
Line 10 is the line that I declare B myB is A.h.

If I comment out theFunction in B and I comment out the include of A.h, it'll compile no problem. But that obviously isn't the solution as B can't
use A.

So, I need one of 2 solutions. Either (and most
preferably) I somehow gain
access to the parent object A's member variables,
without having to pass A as a parameter to B's function....or, I keep it like it is, but somehow get
it to compile.

Can anyone inform me how either of the above can be
accomplished?
Oh...and I know I could just pass myA.x, but don't want to do that as the real function is called often and is actually dependent upon many member variables of A.

Thanks a lot for any help!


Something I have done in the face of circular dependencies

is as illustrated below. Im not especialy proud of it but it worked. If anyone has comments on the technique Id be glad to hear them.

---- A.h ----

#ifndef A_H
#define A_H

class B; /* #include "B.h" */

class A
{ private:
B myB;
....
};
....

/**
* By this point itll be possible to define the
* B class since A has already been defined
*/
#include "B.h"

#endif

---- B.h ----

#ifndef B_H
#define B_H

#include "A.h"

class B
{ public:
void theFunction(A*) ;
....
};
....

#endif
----


You've got the forward declaration and #include-in-header
backwards.
Jul 19 '05 #6
"TR" <no@spam.com> writes:
"Victor Bazarov" <v.********@att Abi.com> wrote in message
news:PN******** ***********@rwc rnsc51.ops.asp. att.net...
If A.h includes B.h and B.h includes A.h, then those errors will be

there.

Of course. The solution is not to have those circular includes.


A better long-term solution is header guards, unless this is the most
complex program he's ever going to create.

Use this in your A.h and B.h files:

#ifndef FILENAME_H
#define FILENAME_H

contents of header

#endif

or in VC++ just:
#pragma once

at the top. But it's less portable.


Huh? Include guards don't prevent circular includes - have you
actually *READ* the thread?

regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frank DOT schmitt AT 4sc DOT com
Jul 19 '05 #7
mjm
I haven't read the whole thread -- so I am sorry if the question has
been answered already.

When the class A is declared in a header pointer members of A need not
have been defined (only declared) a this point:

file A.h:

class B; // forward declaration

class A {

B* b;
int f(B x);
}
The forward declaration allows you to use the name "B"
as a type name, A.h need not include B.h.
The class B has to be defined somewhere of course.
You cannot USE B in the header, ie. this won't work

class A {

B* b;
int f(B x){ return b.doSomething() ; }

}

But if the header contains only declarations and no implementations
that problem does not occur. You move the definition of A::f to A.cc
and #include B.h in A.cc.
Jul 19 '05 #8

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

Similar topics

5
2761
by: dharmesh Gupta | last post by:
i have a multifile program namely bpl.cpp-contains main() function idr.h ( class definitions) idr.cpp ( the implementation of the functions in the classes described in idr.h) bpl1.h ( contains declaration of a global variable) The code from each file is pasted here, ///////////////////////////////////////////
9
2994
by: Sarath | last post by:
I am working with an application using ASP, getting below error when i am trying to include new asp include file. _____________________________________________________________________ Microsoft VBScript runtime error '800a0006' Overflow: '' ____________________________________________________________________ I am sure that there is no error in the new asp file as if i delete any existing include file then no issues.
11
1935
by: cppaddict | last post by:
Say that your CustomClass.h header files requires #include <string> Now say that your CustomClass.cpp file also requires string. Is it good form to repeat the <string> include to make the dependency explicit, or do just allow the include to be make implicitly through the .h include? That is, should the header of your .cpp file be: #include "CustomClass.h"
2
5615
by: puzzlecracker | last post by:
after reading some of the post I found out something rather radical to my previous understanding: that when you do #include<iostream> #include<string> #include<vector> etc compiler puts those .h files into the namespace std -.h files that contain all the declarations for vector, string, etc...
8
2572
by: David D. | last post by:
In html, one can say <script language="JavaScript" src="http://someCodeSnippet.js"> Is there any way to embed the included code snippet in a function (in the case where it is not already a function in the remote souce file)? - David D.
8
3047
by: nrhayyal | last post by:
Hi c++ Gurus, Need your blessing. while testing few aspects with respect to header file inclusions, i observed few things which i would like to share with you. i have a file sqlca.h in which a structure sqlca is declared. i included this file as a soft link in /usr/include. the soft link is as follows: sqlca.h -> /usr/opt/db2_08_01/include64/sqlca.h
4
5292
by: none | last post by:
Hi, I am wondering if anyone has some insight into a way to get the names of the functions that are defined in a C header file. What I am trying to do is develop tests for a large amount of C files. i have written a Perl script to traverse the directory tree looking for ..h files. i find them and then comes the hard part. I have found that searching for ";" and then going backwards looking for close and then open parens gets me to the...
6
1794
by: przemek | last post by:
I used to think that including header files gives me some extra function to use. Like putting <stdlib.h> allows use of system("PAUSE") without doing anything special. I disovered lately that it doesn't work that way with my own header files. With main function in file A, extra functions in file B (both including header file C with declarations of functions in B) to use these functions in A I still need to link A and B - what raises the...
3
2567
by: KIRAN | last post by:
Hello all, My question is about the way of including header files(*.h) in source files (*.c) I have three folders, -build ( for project makefiles) -include ( for *.h files) -src (for *.c files). I know that there are two ways of specifying include path of header files
0
8356
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
8869
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
8781
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
8551
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
8639
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...
1
6198
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
5664
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
4198
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...
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.