How to solve Boost::BGL template<->class circular dependency?

六月ゝ 毕业季﹏ 提交于 2019-12-04 13:20:50

You have circularly included headers. Lane includes graphdefinitions, which includes lane, which includes graphdefinitions, etc. This is the cause of your problem.

Edit: I realized this was already mentioned in the OP. The solution to this problem is PIMPL.

Edit: What I would actually do is put the typedef inside the Lane class. That should solve the problem in the neatest way.

There is a not-well-documented traits class in BGL that gives the vertex and edge descriptor types for an adjacency_list graph without needing to know the property types. It is designed for exactly the use case you have. Look in the "Associated Types" section of http://www.boost.org/doc/libs/1_45_0/libs/graph/doc/adjacency_list.html and notice that there are two definitions for vertex_descriptor and edge_descriptor; you can use the versions that come from adjacency_list_traits in the definitions of your property bundles without causing a circular definition.

I really don't think you need anything special about this code. You have to make sure the definitions of the types used in the graph are declared (not only forward-declared).

@DeadMG: I used a PIMPL-like approach now and I think that this solved my problem.

So what did I do? I changed my Lane-class to look this way:

//lane.hpp
#include "graphdefinitions.hpp"

class LaneSide;
class Lane {
public:
    const LaneSide getLeft() const;
    const LaneSide getRight() const;
    ...
private:
    LaneSide *left;
    LaneSide *right;
    ...
};

And the LaneSide-class which is practically just an indirection and holds the type of value that I could not forward declare inside of lane.hpp, looks this way:

//laneside.hpp
class LaneSide
{
    edge_descriptor* edge;
};

This seems to trick the compiler as I intended to. So thank you for the hint DeadMG. What I was wondering: Is it also possible to store a LaneSide-object inside of the Lane-class not as a pointer but rather as a real object? I tried this first but the compiler complained about the construction. And I'm also wondering whether there might be a way to avoid the additional memory consumption. When my graph gets big enough this might eventually become relevant.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!