C ++枚举问题和g ++中可能的循环依赖

时间:2013-06-14 21:33:14

标签: c++ enums compiler-errors g++ circular-dependency

首先,我为稍微长一点的代码道歉...有3个类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;
}

由于可能的循环依赖(我的估计),当代码是库的一部分时,会发生以下的错误:

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

但是,将代码提取到示例中,错误无法再现。

VS2012编译器在两种情况下运行良好......

我没有,如何解决这类问题?这是一个使用extern的好方法吗?

显然很难提出建议;特别是,当错误无法再现时......

感谢您的帮助......

1 个答案:

答案 0 :(得分:1)

您是否尝试将类ABC放入单个命名空间,然后在命名空间本身内声明枚举?我不确定这是否可以解决您的问题,但它可能有助于清除一些循环依赖。

此外,A似乎并非依赖于C,因此您应该从#include C.h'A.hpp中移除main.cpp行,包括C.h而不是A.h

希望这有帮助。