-2

Sorry for the confusing title but I could not think of a better way to word my question.

I have a class Foo which is declared in the file Foo.cpp.

Foo.cpp however, includes file Bar.h and class Bar tries to create a new instance of object Foo. Of course, I get an undefined identifier error. I need to include both headers in each file because I need to access some static members from each.

Is there any way to achieve this functionality?

Example: Foo.h

#ifndef FOO_H
#define FOO_H

#include "Bar.h"

class Foo 
{
public:
    Foo()
    {
        _newNum = Bar::_num;
    }
private:
    int _newNum
};

Bar.h

#ifndef BAR_H
#define BAR_H

#include "Foo.h"    

class Bar
{
public:
    static int _num;

private:
    Foo f; // Error occurs here, Foo is undefined according to Foo.h.
};

EDIT: AHHH sorry for a very poorly written question. I am feeling very tired. Fixed.

1 Answers1

1

As far as your example goes, there is no reason why Foo.h needs to #include Bar.h.

To fix:

#include "Foo.h"

at the top of Bar.h, below the include guards.

Hugo K
  • 75
  • 6