首页 > 解决方案 > C++中的前向声明和循环依赖

问题描述

我正在尝试在 C++ 中实现一个简单的关系。它是这样的:

车道.h

#include "Teleport.h"
    class Lane
    {
    public:
        std::map<long long, object> object_container;
        std::map<long long, Teleport> teleports_container;

        void add_Object(///) { object_container.emplace(//object//); }
        void add_Teleport(const Teleport &t) { teleport_container.emplace(t._uid, t); }

        Object &get_Object(long long uid) { return object_container.at(uid); }
        Teleport &get_Teleport(long long uid) { return teleports_container.at(uid); }
    }

传送.cpp

#include "Lane.h"
#include "Teleport.h"

    void Teleport::teleport_Object(long long tp_uid)
    {
        _destination.add_Object(_origin.get_Object(tp_uid));
    }
}

传送.h

  class Lane;

  class  Teleport
   {
    public:
        const Lane &_origin;
        const Lane &_destination;

        Teleport(const Lane &origin, const Lane &destination)
            : _origin{origin}, _destination{destination}{}

        void teleport_Object(long long tp_uid);
    };

Teleport.cpp 的编译错误“错误:不完整类型'const class Lane'的无效使用”不胜感激。

标签: c++

解决方案


推荐阅读