473,847 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot compile simple example

Mtk
Hi!
Why does the following, simple, example produce such errors?
I know it has to do with the two header files including each other and
(moreover) the usage of the classes One and Two in the "opposite" header
file... What do I have to do to make this example work?

Thanks for all the help!

// * Error messages:
One.h(11): error C2146: syntax error : missing ';' before identifier 't'
One.h(11): error C2501: 'One::Two' : missing storage-class or type
specifiers
One.h(11): error C2501: 'One::t' : missing storage-class or type specifiers
Two.h(11): error C2146: syntax error : missing ';' before identifier 't'
// * File One.h
#ifndef ONE_H
#define ONE_H

#include "Two.h"

class One
{
public:
One();
private:
Two t;
};

#endif

// * File One.cpp
#include "One.h"

One::One()
{
}

// * File Two.h
#ifndef TWO_H
#define TWO_H

#include "One.h"

class Two
{
public:
Two();
private:
One t;
};

#endif

// * File Two.cpp
#include "Two.h"

Two::Two()
{
}

// * File Main.cpp
int main()
{
return 0;
}
Oct 10 '05 #1
13 1679
I'm new to this, too. So bear with me.

But your header files are CIRCULAR.

one defines the other. Walk through line by line and there is great
confusion.

You must straighten out the sequence of instructions so there is NO
ambiguity. Then it looks like it should compile ok.

- Robert -

Oct 10 '05 #2
"Mtk" <sp***********@ nra.org> wrote in message
news:di******** **@news.island. liu.se...
Why does the following, simple, example produce such errors?
I know it has to do with the two header files including each other
Because of the "include guards," the two files only look like including each
other. In effect, one of them includes the other one; and that's it. (Second
one includes the first one too, but at that time the body of the first
header is included as nothing.)
and (moreover) the usage of the classes One and Two in the "opposite"
header file... What do I have to do to make this example work?
You have to use a forward declaration and must use one of the classes as
reference (meaning both reference and pointer) in the other header.
// * Error messages:
One.h(11): error C2146: syntax error : missing ';' before identifier 't'
One.h(11): error C2501: 'One::Two' : missing storage-class or type
specifiers
One.h(11): error C2501: 'One::t' : missing storage-class or type
specifiers
Two.h(11): error C2146: syntax error : missing ';' before identifier 't'
// * File One.h
#ifndef ONE_H
#define ONE_H

#include "Two.h"

class One
{
public:
One();
private:
Two t;
That line is good; because we know what Two is.
};

#endif

// * File One.cpp
#include "One.h"

One::One()
{
}

// * File Two.h
#ifndef TWO_H
#define TWO_H

#include "One.h"
The line above is ineffective, because the macro ONE_H is defined at this
point, so what we include with the line above is "nothing."
class Two
{
public:
Two();
private:
One t;
You have a conceptual problem on the line above. It is not possible to have
two objects include each other; that would be infinite recursively.

At least one of your classes must "refer" to the other one; it should not
include it as a part.
};

#endif


Without knowing your exact requirements, here is one solution:

Don't include "One.h" in Two.h; instead, forward declare One. Then, keep a
reference to a One object in Two, don't contain one:

// * File Two.h
#ifndef TWO_H
#define TWO_H

class One; // <-- Forward declaration

class Two
{
public:
explicit Two(One & one) // <-- Take a reference
:
t(one) // <-- Remember your One
{}

private:
One & t; // <-- Keep a reference

};

#endif

By the way, I think this must be an FAQ.

Ali

Oct 10 '05 #3
Mtk wrote:
Why does the following, simple, example produce such errors?
I know it has to do with the two header files including each other and
(moreover) the usage of the classes One and Two in the "opposite" header
file... What do I have to do to make this example work?
You can't.

Let's condense your example to one file. And let's suppose the compiler
knows about classes 'One' and 'Two' somehow _apriori_.
[...]
class One
{
public:
One();
private:
Two t;
};
[...]
class Two
{
public:
Two();
private:
One t;
};


Now, what is the size of a class One object? As soon as you can come up
with a formula the compiler can use to calculate it, let us know.

Meanwhile, look up "forward declaration".

V
Oct 10 '05 #4
Mtk
Thanks for the reply.

I may be a newbie when it comes to c++, but there must be a simpler
solution to my problem than the one you described.

Not including "One.h" in "Two.h", and attempting to forward-declare
class One, gives the error message "Two.h(10): error C2079: 'Two::t'
uses undefined class 'One'" - of course.

Anyway. I'll take a look at forward declarations.

Thanks again

Ali «ehreli wrote:
[huge snip]
Without knowing your exact requirements, here is one solution:

Don't include "One.h" in Two.h; instead, forward declare One. Then, keep
a reference to a One object in Two, don't contain one:

// * File Two.h
#ifndef TWO_H
#define TWO_H

class One; // <-- Forward declaration

class Two
{
public:
explicit Two(One & one) // <-- Take a reference
:
t(one) // <-- Remember your One
{}

private:
One & t; // <-- Keep a reference

};

#endif

By the way, I think this must be an FAQ.

Ali

Oct 10 '05 #5
Mtk
Hi!

I forgot to mention that I'm a newbie and I have no intention to make
the compiler calculate the size of any class at all (at least for now)
and, thus, I'll skip that part.

I just want class One to have a variable of the "class Two"-type and
vice-versa, giving the requirement that each class is located in its own
header file.

I'll look "forward declarations" up.

Thanks

Victor Bazarov wrote:
Mtk wrote:
Why does the following, simple, example produce such errors?
I know it has to do with the two header files including each other and
(moreover) the usage of the classes One and Two in the "opposite"
header file... What do I have to do to make this example work?


You can't.

Let's condense your example to one file. And let's suppose the compiler
knows about classes 'One' and 'Two' somehow _apriori_.
> [...]
class One
{
public:
One();
private:
Two t;
};
> [...]
class Two
{
public:
Two();
private:
One t;
};


Now, what is the size of a class One object? As soon as you can come up
with a formula the compiler can use to calculate it, let us know.

Meanwhile, look up "forward declaration".

V

Oct 10 '05 #6
"Mtk" <sp***********@ nra.org> wrote in message
news:di******** **@news.island. liu.se...
Thanks for the reply.

I may be a newbie when it comes to c++, but there must be a simpler
solution to my problem than the one you described.
There isn't a simpler solution. We must break the circular dependency
somehow.
Not including "One.h" in "Two.h", and attempting to forward-declare class
One, gives the error message "Two.h(10): error C2079: 'Two::t' uses
undefined class 'One'" - of course.
Did you make Two::t a reference? (See my code below.)
Anyway. I'll take a look at forward declarations.


Here is an excellent read:

http://www.gotw.ca/gotw/007.htm

Check out the rest of the GotW archive as well :)

[...]
// * File Two.h
#ifndef TWO_H
#define TWO_H

class One; // <-- Forward declaration

class Two
{
public:
explicit Two(One & one) // <-- Take a reference
:
t(one) // <-- Remember your One
{}

private:
One & t; // <-- Keep a reference

};


Ali

Oct 10 '05 #7
Mtk wrote:

Victor Bazarov wrote:
Mtk wrote:
Why does the following, simple, example produce such errors?
I know it has to do with the two header files including each other
and (moreover) the usage of the classes One and Two in the "opposite"
header file... What do I have to do to make this example work?

You can't.

Let's condense your example to one file. And let's suppose the compiler
knows about classes 'One' and 'Two' somehow _apriori_.
> [...]

class One
{
public:
One();
private:
Two t;
};

> [...]

class Two
{
public:
Two();
private:
One t;
};

Now, what is the size of a class One object? As soon as you can come up
with a formula the compiler can use to calculate it, let us know.

Meanwhile, look up "forward declaration".

V


[top posting corrected] Hi!

I forgot to mention that I'm a newbie and I have no intention to make
the compiler calculate the size of any class at all (at least for now)
and, thus, I'll skip that part.
That's not a good idea; understanding this is *absolutely central* to
your understanding of why you can't do what you're trying to do.
I just want class One to have a variable of the "class Two"-type and
vice-versa, giving the requirement that each class is located in its own
header file.
Right. See above.
I'll look "forward declarations" up.


HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Oct 10 '05 #8
On Mon, 10 Oct 2005 21:08:18 +0200, Mtk wrote:


I just want class One to have a variable of the "class Two"-type and
vice-versa, giving the requirement that each class is located in its own
header file.


But then a "One" object (which contains a "Two" object containing a "One"
object) would then, within itself, contain another complete One object.
Which contains a Two which contains a One which...

The only way this can make sense is if a One object is of infinite size.

- Jay
Oct 10 '05 #9
Mtk
Thanks!

I'm reading on ;)

Ali «ehreli wrote:
"Mtk" <sp***********@ nra.org> wrote in message
news:di******** **@news.island. liu.se...
Thanks for the reply.

I may be a newbie when it comes to c++, but there must be a simpler
solution to my problem than the one you described.


There isn't a simpler solution. We must break the circular dependency
somehow.
Not including "One.h" in "Two.h", and attempting to forward-declare
class One, gives the error message "Two.h(10): error C2079: 'Two::t'
uses undefined class 'One'" - of course.


Did you make Two::t a reference? (See my code below.)
Anyway. I'll take a look at forward declarations.


Here is an excellent read:

http://www.gotw.ca/gotw/007.htm

Check out the rest of the GotW archive as well :)

[...]
// * File Two.h
#ifndef TWO_H
#define TWO_H

class One; // <-- Forward declaration

class Two
{
public:
explicit Two(One & one) // <-- Take a reference
:
t(one) // <-- Remember your One
{}

private:
One & t; // <-- Keep a reference

};


Ali

Oct 10 '05 #10

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

Similar topics

8
2819
by: janeaustine50 | last post by:
Python's InteractiveInterpreter uses the built-in compile function. According to the ref. manual, it doesn't seem to concern about the encoding of the source string. When I hand in an unicode object, it is encoded in utf-8 automatically. It can be a problem when I'm building an interactive environment using "compile", with a different encoding from utf-8. IDLE itself has the same problem. ( '<a string with non-ascii-encoding>' is...
1
24810
by: Tony Johansson | last post by:
Hello! I get compile error when compiling using the command javac from the command terminal window(CMD). I have just two classes which are called HelloWorld.java and Slask.java. I have both classes in the directory called temp and I do cd temp to this directory. Then I do javac HelloWorld.java Now I get the compile error HelloWorld.java:8 cannot resolve symbol
8
5486
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
2
6938
by: PeterW | last post by:
I have an xml file from which I want to generate an xsd schema and at a later stage a cs class. The xml file has a mix of defined namespaces and also an empty namespace. These are defined as follows: <silcn:silcn xmlns:silcn='http://silcn.org/200309' xmlns='http://xmlprobe.com/200312'> it contains an element <report> off the root and also a separate <Silcn:report> again off the root.
10
4482
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat Enterplise Linux 3 ES, and applied FixPack fp5_mi00069.tar to it. After creating an instance, starting the database, creating a database, and entering the table definitions, all of which seems to work OK, I entered a tiny 8-row table and can do...
10
4796
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data structures are read only) Ideally one should be able to put the redundant information there automatically so no mistakes are possible, but in a lot of case I see no way how to do it.
5
5052
by: wong_powah | last post by:
#include <vector> #include <iostream> using std::cout; using std::vector; enum {DATASIZE = 20}; typedef unsigned char data_t;
45
18940
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies of their elements? Why can't I change the element itself? class Program { private struct MyStruct
17
3123
by: fl | last post by:
Hi, I am learning C++ from the following C++ website, which has some very good small examples. For the second Fraction example, which has five files: Main.cpp Fraction.cpp Fraction.h msoftcon.cpp msoftcon.h
0
9887
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9730
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
10983
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
9485
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...
1
7883
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
7056
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
5911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4529
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3164
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.