0

At first I am apologize for a slightly longer code... There are 3 classes A,B,C

A.h

#ifndef A_H
#define A_H

template <typename T>
class C;

class A
{
public:
    template <typename T>
    static void testa ( T b);
};

#include "A.hpp"

#endif

A.hpp

#ifndef A_HPP
#define A_HPP

#include "C.h"
#include "B.h"

template <typename T>
void A::testa ( T a)
{
    B::testb( a );
}

#endif

B.h

#ifndef B_H
#define B_H

class B
{
    public:

            template <typename T>
            static void testb ( T b );
};

#include "B.hpp"

#endif

B.hpp

#ifndef B_HPP
#define B_HPP

#include "C.h"

template <typename T>
void B::testb ( T b )
{
    C <T>::test(b, e1 );  //Error
}

#endif

C.h

#ifndef C_H
#define C_H

#include "A.h"

typedef enum
{
    e1=1, e2,
} TEnum;


template <typename T>
class C
{
 public:
    static void test (T c, const TEnum t) {}
};

#endif

main.cpp

#include "A.h"

using namespace std;

int main()
{
    double x = 1.0;
    A::testa(x);
return 0;
}

Due to the possible circular dependency (my estimation), when the code is a part of the library, the following eeror occurs:

 C <T>::test(b, e1 ); |261|error: 'e1' was not declared in this scope|.

However, extracting the code to the example, the error can not be reproduced.

The VS2012 compiler works well in both cases...

I do not, how to fix such a type of the problem? Is it a good way to use extern?

It is clear that it is difficult to advise; especially, when the error can not be reproduced...

Thanks for your help...

justik
  • 4,145
  • 6
  • 32
  • 53
  • you don't need the `include "A.h"` in `C.h`. – didierc Jun 14 '13 at 21:41
  • Also `typedef enum` is idiomatic C, not C++. Just use `enum TEnum` – Mark B Jun 14 '13 at 21:42
  • you don't need the `include "C.h"` in `A.hpp` either. Always include the minimum headers necessary for your code to remain consistent. Use forward declarations as much as possible instead of includes, but don't over declare either if it's not necessary. – didierc Jun 14 '13 at 21:44
  • you could drop the forward declaration of C in the headers of A, since the later does not manipulate the former in any way. – didierc Jun 14 '13 at 21:47
  • @ didierc: Unfortunately I need that. There some C components using in A.hpp methods... – justik Jun 14 '13 at 21:51

1 Answers1

1

Have you tried putting classes A, B, and C into a single namespace, and then declaring your enum inside the namespace itself? I'm not sure if this would resolve your problem but it may help clear up some circular dependencies.

Also it would appear that A is not in fact dependent on C, so you should remove the line #include C.h' from A.hpp and, in main.cpp, include C.h instead of A.h.

Hope this helps.

Forest Kunecke
  • 2,160
  • 15
  • 32