我们在代码库中编写了一个RefCounted
对象,如下所示;我们有一个类型参考周期,所以我们前进定义MyCountedObject
任何人都可以解释为什么存在类型冲突,以及如何实现这种类型的循环引用?
代码段:
// util.cpp
namespace util {
template <typename T, typename DeletorT>
class RefCounted: public T {
};
template <typename T>
class Deletor {
};
}
namespace ns {
class Object {
};
class CX {
class MyCountedObject;
typedef util::RefCounted<Object, util::Deletor<MyCountedObject> > MyCountedObject;
};
}
但是编译会引发以下错误:
example.cpp:16:69: error: typedef redefinition with different types ('util::RefCounted<Object,
util::Deletor<MyCountedObject> >' vs 'ns::CX::MyCountedObject')
typedef util::RefCounted<Object, util::Deletor<MyCountedObject> > MyCountedObject;
^
example.cpp:15:9: note: previous definition is here
class MyCountedObject;
^
1 error generated.