【发布时间】:2016-04-08 19:49:26
【问题描述】:
我有几个类(A、B、C),每个类都有一个模板(Ptr<...>)类型的成员,具体取决于另一个类(循环)的不完整类型。我想键入定义类型(::ptr),如下所示。这似乎不起作用 - 我的编译器告诉我以下内容:
In file included from B.hpp:6:0,
from A.hpp:6:
C.hpp:13:8: error: ‘ptr’ in ‘class A’ does not name a type
A::ptr a;
^
但是,使用 T* 而不是 T::ptr 可以使其正常工作。我该如何解决?
A.hpp:
#ifndef TEST_INCLUDE_A
#define TEST_INCLUDE_A 1
class A;
#include "B.hpp"
#include "P.hpp"
class A {
public:
typedef Ptr<A> ptr;
B::ptr b;
};
#endif
B.hpp:
#ifndef TEST_INCLUDE_B
#define TEST_INCLUDE_B 1
class B;
#include "C.hpp"
#include "P.hpp"
class B {
public:
typedef Ptr<B> ptr;
C::ptr c;
};
#endif
C.hpp:
#ifndef TEST_INCLUDE_C
#define TEST_INCLUDE_C 1
class C;
#include "A.hpp"
#include "P.hpp"
class C {
public:
typedef Ptr<C> ptr;
A::ptr a;
};
#endif
P.hpp:
#ifndef TEST_INCLUDE_PTR
#define TEST_INCLUDE_PTR 1
template<class T>
class Ptr {
public:
T* ptr_t;
};
#endif
【问题讨论】:
标签: c++ class templates typedef circular-dependency