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

Compiling problem

I'm having a quite complicate situation, and it doesn't want to
compile.
The situation is as follows:
I have three classes, let's call them A, B and C. They each have a .h
and a .cpp file.
All headers use the following construct to make sure the headers are
not included multiple times:
#ifndef _A_H_
#define _A_H_
class A {
...
};
#endif
A has a static member of class B,
B has a member of class C and works with objects of class A in some
methods,
and C has a member of class A.

When i try to compile this, i get errors saying that there is no class
A when C is compiling, and so on. How could i fix this?

Mar 18 '06 #1
2 2273
* Barrie:
I'm having a quite complicate situation, and it doesn't want to
compile.
The situation is as follows:
I have three classes, let's call them A, B and C. They each have a .h
and a .cpp file.
All headers use the following construct to make sure the headers are
not included multiple times:
#ifndef _A_H_
Names starting with an underscore followed by uppercase are reserved for
the implementation; you have Undefined Behavior already.
#define _A_H_
class A {
...
};
#endif
A has a static member of class B,
B has a member of class C and works with objects of class A in some
methods,
and C has a member of class A.

When i try to compile this, i get errors saying that there is no class
A when C is compiling, and so on. How could i fix this?


The best way: fix your design, which is hopeless, + read up on C++ as a
language, where you need to learn such basic things as naming rules (see
above).

Or technically, which is the worst you can do, include the proper
headers at the right places and use forward declarations to make your
current bad/evil design compile; see the FAQ for example(s). If you
choose to do this, then there is a good chance that after it compiles,
and after fixing the name issue mentioned above, you'll still have UB
due to static initialization order problems. And there is an even
better chance that you won't notice until something else blows up.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 18 '06 #2
On 18 Mar 2006 02:58:29 -0800, "Barrie" <ba********@gmail.com> wrote:
I'm having a quite complicate situation, and it doesn't want to
compile.
The situation is as follows:
I have three classes, let's call them A, B and C. They each have a .h
and a .cpp file.
All headers use the following construct to make sure the headers are
not included multiple times:
#ifndef _A_H_
#define _A_H_
class A {
...
};
#endif
A has a static member of class B,
B has a member of class C and works with objects of class A in some
methods,
and C has a member of class A.

When i try to compile this, i get errors saying that there is no class
A when C is compiling, and so on. How could i fix this?
You will always have circular dependency issues if you have these
objects as members. As Alf P. Steinbach has pointed out, a design like
this will never work.

When a class declaration is compiled, the compiler must know how large
the object will be by the time the semicolon after the closing brace
is reached. That means that for all class members, the size of each
member must be known. If you have a pointer or reference member, the
compiler can accept it without including that type's header because
the size of a pointer is known without knowing the type (except for
pointer-to-member under certain circumstances). All one must do is to
write a forward-declaration of the class' name, e.g.:

class A;

class B {
// B can use the name, but not the type:
A* pa;
};

For member variables of class type, each header will have to include
the headers for those members. IOW,
A has a static member of class B, ....means that header for A must include header for B;
B has a member of class C and works with objects of class A in some
methods, ....means that B's header must include C's header, but not necessarily
A's header because it isn't needed until the definitions of the
methods (which you should put in a separate .cpp file);
and C has a member of class A.

....means that C's header must include A's header. But wait a minute
.... A needs B, which needs C, which needs A ... you see where this is
going? It is a circular dependency.

One possible solution, if you cannot avoid the dependencies, is to use
pointer or reference members instead of objects, and forward-declare
the class instead of including the class header. You will eventually
need to include the headers, but you can include them in the .cpp
file.

--
Bob Hairgrove
No**********@Home.com
Mar 18 '06 #3

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

Similar topics

0
by: Nikola Milutinovic | last post by:
Hi all. I'm not subscribed to the list, so all replies to me mail directly. I ran into a small problem, for which we have found a workaround. I was compiling PostgreSQL v7.4.1 and it's...
0
by: Martin Bless | last post by:
I need to access a MSSQL database (MS-Sql, not MySQL!)and would very much like to use mssql-0.09.tar.gz which is available from http://www.object-craft.com.au/projects/mssql/download.html ...
11
by: Arturo DiDonna | last post by:
Hello everyone. I am trying to compile someone else code and I am stuck with compilation problems using the g++ 3.3 compiler. Basically, when compiling the following code, I get this error...
3
by: modemer | last post by:
Hello, I got weird compiling message similar like following when I compiled my simple code on Sun 5.8 with CC WorkShop 6 update 2 C++ 5.3. CC -g -o myclass.o -c myclass.cpp CC -g -o main.o...
0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
3
by: Dmitri Shvetsov | last post by:
Hi All, Did somebody have a problem compiling some solution in VS2003? I get the error every time that some files are locked and are using by another process. I repeat compiling again and again...
4
by: Aaron Queenan | last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the following error message: Linking... LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport LINK : error...
2
by: Rudy Ray Moore | last post by:
Hi guys, I just upgraded to "Visual Studio .net 2003 7.1 c++" from VS6. Some things I like (proper for loop variable scoping, for example), but some other things are troubling me. One...
0
by: erkidevries | last post by:
Problem compiling Tkinter program with bmp images (using py2exe) I have a Tkinter gui program that uses bmp as backgrounds. The program itself works when I run from the source. I placed the...
8
by: WebSnozz | last post by:
I have an application written in C that does a lot of low level stuff. It does a lot of things like casting from void*'s. I want to create a new GUI for it in either C# or MC++, but reuse the...
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
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
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
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
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,...
0
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...

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.