Noah
Noah

Reputation: 184

How do I define an object in the header file? (without using it)

Sorry if the question title isn't confusing but I'm not sure how to word it better.

Basically, here is my code.

Header File

class Foo {
     public: 
          Foo();
     private: 
          Bar * b;
}

class Bar {
     public:
          Bar(Foo *f);
     private:
          Foo * foo;
}

cpp File

Foo::Foo() {
     new Bar(this);
}

Bar::Bar(Foo * f) {
     foo = f;
}

I am trying to pass the values to each other back and forth but Bar is not declared to Foo. How do I make Bar known to Foo?

Upvotes: 0

Views: 39

Answers (1)

i_am_jorf
i_am_jorf

Reputation: 54600

class Bar;  // <-- Forward declaration.

class Foo {
     public: 
          Foo();
     private: 
          Bar * b;
}

Upvotes: 3

Related Questions