![]() |
|
#1
|
|||
|
|||
can't figure this out (classes cross referencing)Hi
I am trying to figure out how this can be done, as i am sure it can... I am trying to define 2 classes (let's call them A and B). Now, A has a data memeber of class B, and B has method that reuturn a type A variable. these are stored in 2 different header files. lets make it more understandable: A.h: CPP / C++ / C Code:
B.h CPP / C++ / C Code:
both headers have an include guard. Now, the problem is i cant get them both to compile, as each one is dependant on the other and can't find it no matter what i do. I am looking for a way to say "class A exists, carry on compiling" like an extern command... Thanks in advance, hope it makes sense.... Ehud. |
|||
|
#2
|
||||
|
||||
Re: can't figure this out (classes cross referencing)A few things:
1. B* b is missing the final ';' 2. both classes need to also end with ';' after the last bracket. (without the single quotes, of course) 3. no constructors/destructors? 4. where is getA() defined? __________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! |
|
#3
|
|||
|
|||
Re: can't figure this out (classes cross referencing)5. the file name to be included must be surrounded by < and > (to look in the standard header locations) or in this case "A.h" (to look in the source code directory)
The directive you are looking for is called a "forward declaration." At the beginnings of the header files, you can put CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
Re: can't figure this out (classes cross referencing)Quote:
If a class definition has a member that involves a pointer to another class, you have to tell it that it is a class (It doesn't have to know any details about the other class). Here's what I mean (without actually showing the class implementation or object instantiation, just make sure that you can define each with a reference to the other). Note that a.h doesn't have to #include b.h, and b.h doesn't have to #include a.h CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
Regards, Dave |
|
#5
|
|||
|
|||
Re: can't figure this out (classes cross referencing)Thanks Dave, that's exactly what i was looking for.
BTW - VC++ will let you define "extern class a;" as well and it will compile fine. other compilers will give an error... Ehud. |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Photoshop Tutorial: Make An Inspirational/Mystical Picture | ToddSAFM | Graphics Forum | 9 | 09-Aug-2005 21:32 |
| Assistance with classes... | Bravebird | C++ Forum | 7 | 27-Apr-2005 14:17 |
| Fairly simple classes help please | sammacs | C++ Forum | 0 | 30-Nov-2004 10:58 |
| Cross include | BlueTeeth | C++ Forum | 3 | 24-Mar-2004 21:37 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The