具有完整类型信息的循环依赖项
Circular dependency with full type information
我有 2 个类,它们都包含在变体中。然后,该变体在两个类中使用。
template <typename T>
struct Deferred
{
typedef T value_type;
};
template <typename T>
struct DeferredContainer
{
typedef typename T::value_type value_type;
};
class DictionaryObject;
class MixedArrayObject;
using DictionaryObjectPtr = Deferred < DictionaryObject >; // DeferredContainer<DictionaryObject>
using MixedArrayObjectPtr = Deferred < MixedArrayObject >; // DeferredContainer<MixedArrayObject>
typedef boost::variant <
MixedArrayObjectPtr,
DictionaryObjectPtr
> ContainerPtr;
class MixedArrayObject
{
public:
typedef int value_type;
inline void SetContainer(ContainerPtr obj) { _container = obj; }
inline ContainerPtr GetContainer() const { return _container; }
private:
ContainerPtr _container;
};
class DictionaryObject
{
public:
typedef float value_type;
inline void SetContainer(ContainerPtr obj) { _container = obj; }
inline ContainerPtr GetContainer() const { return _container; }
private:
ContainerPtr _container;
};
我不知道这是如何工作的,但是当我尝试通过 typedef 在数组或字典上公开某些属性(并在 DeferredContainer 中使用它们)时,我收到错误:
错误 C2602:"延迟容器::value_type"不是"延迟容器"基类的成员
我可以以某种方式完成这项工作吗?还有其他想法吗?
更新 31.5,下午 4:09。
我已经将问题减少到最低限度。希望这最终引起注意。代码。
将 MixedArrayObjectPtr 切换为 DeferredContainer 后,代码将不再编译。
我建议重新排序您的代码,以确保所有定义都按照它们的使用顺序排列。由于需要variant
具有完整的类型,因此我们必须使用 Object*
而不是 Object
:
// main types
class Dictionary;
class Array;
template <typename T> struct DeferredContainer;
using DictionaryPtr = DeferredContainer<Dictionary>;
using ArrayPtr = DeferredContainer<Array>;
struct Object;
// now definitions of them
class Dictionary
{
public:
typedef int value_type;
std::map<Name, Object*> list; // note Object*
};
class Array
{
public:
typedef int value_type;
std::vector<Object*> list; // note only one type here
};
template <typename T>
struct DeferredContainer
{
// now can do stuff with T::value_type
};
毕竟,我们可以宣布Object
.不幸的是,我们不能转发声明别名,但我们可以只包装variant
:
struct Object {
boost::variant<
DictionaryPtr,
ArrayPtr
> value;
};
通过这种排序,一切都可以编译。
相关文章:
- C++GTKMM gui循环依赖关系
- 如何在头文件中声明类模板(由于循环依赖关系)
- 如何在具有循环依赖的类中dynamic_cast?
- C++模板方法中的循环依赖关系
- 修复循环依赖项 c++17 标头
- 涉及全局对象的循环依赖C++
- 循环依赖,在继承类的情况下使用覆盖方法
- Wt::D bo 中的循环依赖关系
- 在包含窗口标头时难以解决循环依赖关系问题
- 解决循环依赖关系 c++ 的想法
- C++循环依赖关系,未声明的标识符
- C++ 中的循环依赖关系问题
- 错误 C2512 视觉C++(并且不是循环依赖项)
- CMake 外部和内部静态库的循环依赖关系
- 正在从继承中解析循环依赖项
- "std::shared_ptr"循环依赖关系是如何导致问题的
- 纯引用而不是weak_ptr来打破循环依赖关系
- "invalid use of incomplete type" .解决循环依赖关系
- 如何避免模板方法的循环依赖
- 循环依赖结构,使用前向声明时结构的错误重定义