0
votes

I have difficulties to setup a structure with 3 classes. I'm not sure about includes / forward declaration.

The code below compiles until I add the namespace and enum in inclino.h. I receive "inclino has not been declared" from InclinoMeasure.h

Inclino.h

// Inclino.h
#include "InclinoMeasure.h"
#include "InclinoReading.h"

namespace inclino {
enum a{a1, a2}
}

class Inclino
{
    list<InclinoMeasure*> m_measures;

    void f(inclino::a a);
}

InclinoMeasure.h

// InclinoMeasure.h
#include "InclinoReading.h"

class Inclino;

class InclinoMeasure
{
    Inclino *m_inclino;
    list<InclinoReading*> m_readings;

    void f(inclino::a a);
}

InclinoReading.h

// InclinoReading.h

class Inclino;
class InclinoMeasure;

class InclinoReading
{
    InclinoMeasure *m_inclino;

    void f(inclino::a a);
}

Does the structure is correct ? How can I access the enum in others class ?

Thanks in advance.

Edit : I fixed the namespace issue by putting it in a new file including by the 3 classes. But seems I misused the include / forward declaration.

1

1 Answers

1
votes

I receive "inclino has not been declared" from InclinoMeasure.h

This is because the namespace inclino and enum a are currently defined in Inclino.h, but InclinoMeasure.h does not include Inclino.h (and should not, given the current relationship between the headers, since that would introduce a circular dependency). The forward declaration of class Inclino is irrelevant to the namespace and enum.

To solve this problem, you can move the definition of the namespace and enum to InclinoReading.h.

This will make it available in all three headers where it is used, since both Inclino.h and InclinoMeasure.h include InclinoReading.h.