GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-May-2006, 15:23
ehudros ehudros is offline
New Member
 
Join Date: May 2006
Posts: 6
ehudros is on a distinguished road

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:
#include B.h
class A
{
    B* b
}

B.h
CPP / C++ / C Code:
#include A.h
class B
{
    A* getA();
}

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  
Old 09-May-2006, 16:50
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,141
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

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  
Old 09-May-2006, 17:12
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

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:
class A; //in B.h
class B; //in A.h
  #4  
Old 09-May-2006, 17:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: can't figure this out (classes cross referencing)


Quote:
Originally Posted by ehudros
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.

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:
// a.h
class B; // This file needs to know that B is a class and that's all it needs
         // to know. Think of this declaration as "previews of coming
         // attractions", like at the movies.

class A
{
  // other stuff for class A members

    B* pB;

  // other stuff
};

CPP / C++ / C Code:
// b.h
class A; // This file needs to know that A is a class and that's all it needs
         // to know. Think of this declaration as "previews of coming
         // attractions", like at the movies.

class B
{

  // other stuff for class B members

    A* getA();

  // other stuff
};

CPP / C++ / C Code:
//main.cpp

#include <iostream>

#include "a.h"
#include "b.h"

using std::cout;
using std::endl;

int main()
{

    // create objects from Class A and Class B
    // etc.

    cout << "Included both" << endl;
    return 0;
}

Regards,

Dave
  #5  
Old 12-May-2006, 10:29
ehudros ehudros is offline
New Member
 
Join Date: May 2006
Posts: 6
ehudros is on a distinguished road

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 GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 02:26.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.