forward reference to a class...

F

Francesco

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;
}
 
N

Nils Petter Vaskinn

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 ?
 
R

Rob Williscroft

Francesco wrote in
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-lite/misc-technical-issues.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.
 
F

Francesco

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top