C++ programming question

Suki

you will get nothing
Joined
Oct 23, 2002
Messages
509
Location
canada
it's probably something stupidly simple to fix but I can't find the answer.
it's that have two classes, in each I have a pointer to the other, I can't compile because either way it finds that pointer to a class that isn't defined yet, how do I fix this?

thanks in advance
 
At the top of each class header put 'class <other_class>;'

This forward defines the other class so that this class knows what to do.

So:

Header file foo.h has:
Code:
class Bar;

class Foo {
    Bar* mBar;
}
and header file bar.h has:
Code:
class Foo;

class Bar {
    Foo* mFoo;
}
 
like I said, so simple, it's a lot like a function header...

thanks
 
aparently that's far from my only problem,
I've tyied to follow what little instructions i have for building multi file programs but aparently i'm really not doing something right...

I'm getting a lot of "'fstream' ambigious symbol", "undefined base class","class type redefinition" "missing ; before using" (in 'using namespace std', which needs to be ther for me you use the vector template).. depending which files I include in main.cpp it'll even give me errors in the library file "fstream.h"

what really bothers me is that I know the actual program would need no more than 10 minutes od debugging to run properly but I can't figure out all these file interactions..they're never covered in any of the courses I've taken, and i can't find a clear description anywhere

more help? anyone?
 
and "include files 363 layers deep possible recursion" (yes recursion but I still don't know how to make this work)

some of the problems seem to be coming from the fact that I have each class refering to members of the other like this

Code:
class Bar;

class Foo 
{
    Bar* mBar;
Public:
    void go(void) 
    {
        mbar->go;
    }
}
and
Code:
class Foo;

class Bar 
{
    Foo* mFoo;
public:
    void go(void) 
    {
         mfoo->go;
    }
}

so they need to know more than just that the other class exists,

i suppose I could define a 'top' object class and have them each hold a top*, and define go as a pure virtual function, but I'd still like to know how to do this multiple file thing properly, and to make that top class it would be a lot easier if I knew how use multiple base classes. which i will need to learn eventually but not now.
 
well those 'go' functions would cause an infinte loop...
I don't see any other problems...
 
this is just an example,

and they wouldn't necessarly cause a loop since the pointers aren't necessarly pointing in a circle, what i've got is actually more like a linked list with alternating classes..

anyway like I said i can get aroud that with a 'top' function and 'top*' pointer
but still don't know how to setup a multiple file project properly..
 
In your skeleton code you appear to be including inline code in your header. If you just put declarations in the headers and move method implementations into the .cp files then each .cp file can #include the other header with no circular references.

If you really need circular file references you can use a conditional compiler directive to define a header only if it hasn't already been defined:

In class1.h:
Code:
#ifndef _CLASS1_
#define _CLASS1_

<body of class1 header>

#endif

In class2.h:
[code]
#ifndef _CLASS2_
#define _CLASS2_

<body of class2 header>

#endif

Now you can #include class2.h in class1.h and vice versa.
 
it works now, thankyou all.

those #ifndef #define #endif things were very helpful
i was also #including the .cpp files in my main file when it should have been the .h, that was causing half of the problems..
 
Back
Top Bottom