首先我为稍微长一点的代码道歉…有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
刘昀
#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是一种好方法吗?
很明显,建议是困难的;特别是,当错误不能重现时…
您是否尝试过将类A
, B
和C
放入单个命名空间,然后在命名空间本身内声明枚举?我不确定这是否能解决你的问题,但它可能有助于清除一些循环依赖。
而且看起来A
实际上并不依赖于C
,所以您应该从A.hpp
中删除#include C.h'
行,并且在main.cpp
中包含C.h
而不是A.h
。