I've got a problem with a pair of classes that each refer to the other
class. I've simplified things by writing a couple dummy classes, Foo and
Bar. I've got them set up with a forward declaration for Bar, then Foo's
definition, then Bar's definition:
class Bar;
class Foo{...};
class Bar{...};
Each defines an enumeration. Bar contains members of its own enumeration
type (BarEnum) and Foo's enumeration type (FooEnum). Foo contains a
member that's a pointer of type Bar and a member of type FooEnum. At
this point, no problem. I've pasted my source from this stage below, it
should compile.
But I also want to be able to use BarEnum from within Foo, and I don't
know how to make this work. I've got the lines that I used to try to do
this commented out in the source pasted below. It's pretty clear to me
why this can't possibly work, but is there some way to make something
similar work? Perhaps a way to forward declare some of the innards of
Bar?
I'm starting to think this is a) totally illegal in C++, or b) just
really bad programming.
Any help would be much appreciated!
Jessica
File foo.h:
#ifndef FOO_H
#define FOO_H
class Bar;
class Foo
{
public:
typedef enum {A,B,C} FooEnum;
Foo();
FooEnum getFE() const;
FooEnum getBFE() const;
//Bar::BarEnum getBBE() const;
private:
Bar *b;
FooEnum fe;
};
class Bar
{
public:
typedef enum {D = 10, E, F} BarEnum;
Bar();
BarEnum getBE() const;
Foo::FooEnum getFE() const;
private:
Foo::FooEnum fe;
BarEnum be;
};
#endif
File foo.cpp:
#include <iostream>
using namespace std;
#include "foo.h"
Foo::Foo()
{
fe = A;
b = new Bar();
}
Foo::FooEnum Foo::getFE() const
{
return fe;
}
Foo::FooEnum Foo::getBFE() const
{
return b->getFE();
}
/*Bar::BarEnum Foo::getBBE() const
{
return b->getBE();
}*/
Bar::Bar()
{
fe = Foo::A;
be = D;
}
Bar::BarEnum Bar::getBE() const
{
return be;
}
Foo::FooEnum Bar::getFE() const
{
return fe;
}
int main()
{
Foo f;
Bar b;
cout << "f's FooEnum val: " << f.getFE() << endl;
cout << "f's b member FooEnum val: " << f.getBFE() << endl;
//cout << " and BarEnum val: " << f.getBBE() << endl;
cout << "b's FooEnum val: " << b.getFE();
cout << " and BarEnum val: " << b.getBE() << endl;
}
[Non-text portions of this message have been removed]