473,324 Members | 2,501 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,324 software developers and data experts.

forward reference to a class...

Hi,

I'm not able to make a forward reference to a class, so there's something I
don't know... Can you help me?

Here's the example...

/************************************************** **********
Programma: File: Funzione: Descrizione: Autore:
Ambiente: Dev-C++ 4.9.8.1, Athlon/750 128mb RAM, Windows 98
Note: Revisioni: 16/09/03 11.05
************************************************** **********/
#include <iostream>

class B;

class A {
public:
B first;
};

class B {
public:
A second;
};

int main() {
system("PAUSE");
return 0;
}
--------------------------------------------------------

Thanks in advance,
Checco.
Jul 19 '05 #1
5 9588
Might try storing pointers to classes as members instead.

HTH,
- J.
Jul 19 '05 #2
On Tue, 16 Sep 2003 09:08:34 +0000, Francesco wrote:
Hi,

I'm not able to make a forward reference to a class, so there's something I
don't know... Can you help me?

class B;

class A {
public:
B first;
};

class B {
public:
A second;
};

A contains a B which contains an A which contains a B which contains an A
which ......

See where we're going?

Your problem isn't that you can't tell the compiler what to do, it's that
you are asking it to do something impossible. You probably want first and
second to be pointers, but without telling us what A and/or B are for we
can't know.

What are A and B supposed to be/do ?

--
NPV
"Linux is to Lego as Windows is to Fisher Price." - Doctor J Frink

Jul 19 '05 #3
Francesco wrote in news:op**************@news.tin.it:
Hi,

I'm not able to make a forward reference to a class, so there's
something I don't know... Can you help me?
I've no doubt there is a faq entry for this. Oh here it is:
http://www.parashift.com/c++-faq-lit...sues.html#faq-
38.12

To avoid the wrap: http://tinyurl.com/niru


Here's the example...

/************************************************** **********
Programma: File: Funzione: Descrizione: Autore:

Ambiente: Dev-C++ 4.9.8.1, Athlon/750 128mb RAM, Windows 98
Note: Revisioni: 16/09/03 11.05
************************************************** **********/
#include <iostream>

class B;

class A {
public:
B first;
};
What's the size of A ?

class B {
public:
A second;
};

What's the size of B ?
int main() {
system("PAUSE");
return 0;
}


Both A and B above if the above code was legal would have infanite
size ( A includes B wich includes A ... ).

Which is probably why the language requires a complete type,
one which has a body ( the bit in {}; for a class ) if you want
to have a member of that type. References, pointers and static
members are OK though as the defenition (particulary sizeof info')
isn't required till later.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #4
On Tue, 16 Sep 2003 10:38:42 GMT, Nils Petter Vaskinn
<no@spam.for.me.invalid> wrote:


Your problem isn't that you can't tell the compiler what to do, it's that
you are asking it to do something impossible. You probably want first and
second to be pointers, but without telling us what A and/or B are for we
can't know.

What are A and B supposed to be/do ?


I'm learning C++ and this was only an exercice to take confidence with
class. But I've understood my mistake and effetively pointers are the right
way to make a mutual association between two object.

Thanks,
Checco.
Jul 19 '05 #5
Thank to all,
I've understood my mistake.

Thanks,
Checco.
Jul 19 '05 #6

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

Similar topics

2
by: Mountain Bikn' Guy | last post by:
It is known that one cannot pass arguments as ref or out in a marshal-by-reference class. My problem is that I have a C DLL (and C# wrapper) that I need to isolate in an AppDomain and then I need...
4
by: Max | last post by:
public class Entity : System.Windows.Forms.Label { .... protected System.Drawing.Point lastMousePosition; .... } public class Flow : Entity { ....
7
by: Michael B Allen | last post by:
I have a forward reference like: struct foo; int some_fn(struct foo *param); Because the parameter is a pointer the compiler is satisfied. But now I wan to change 'struct foo' to a...
3
by: yancheng.cheok | last post by:
hello all, how can i make, a forward declaration class's enum member, being visible by another class? consider the following case, ---------------------------- dog.h...
3
by: ApexData | last post by:
Hello My MainForm manages Employee records. I have created another form that manages employee promotions. On the MainForm a button launches the PromotionForm and lists the promotions for the...
2
by: sharuvman | last post by:
HI All, I have a problem with forward referencing : class classB; class A { classB ram; };
8
by: Ole Nielsby | last post by:
I want to create (with new) and delete a forward declared class. (I'll call them Zorgs here - the real-life Zorks are platform-dependent objects (mutexes, timestamps etc.) used by a...
6
by: barcaroller | last post by:
Why does the following code cause a compiler error? class A; // forward reference class B { foo() { a = new A;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.