【问题标题】:boost::graph compilation issue with dynamic_properties and write_graphviz动态属性和 write_graphviz 的 boost::graph 编译问题
【发布时间】:2015-12-08 15:55:17
【问题描述】:

这个问题是关于 boost::graph 以及如何处理与顶点(和/或边)相关的属性。我对处理这个问题感到很困惑,但我怀疑这可能是与模板相关的问题。

假设我有这个图形定义:

struct myVertex_t {
    int color;
};

typedef boost::adjacency_list<
    boost::vecS,                   // edge container
    boost::vecS,                   // vertex container
    boost::undirectedS,            // type of graph
    myVertex_t,                    // vertex properties
    boost::property<               // edge properties
        boost::edge_color_t,             // ???
        boost::default_color_type        // enum, holds 5 colors
    >
> myGraph_t;

AFAIK,这种存储顶点属性的方式称为 "bundle properties" 并且似乎是存储此信息的第三种方式,尽管据说 in the manual 那:

有两种图形属性:内部和外部。

回到我的主要问题。现在,我可以通过这种方式使用“点”格式实例化并打印出一个图形:

 int main()
 {
    myGraph_t g;
    boost::add_edge(0, 1, g);

    boost::dynamic_properties dp;
    dp.property("color",   boost::get( &myVertex_t::color,  g ) );
    dp.property("node_id", boost::get( boost::vertex_index, g ) );
    boost::write_graphviz_dp( std::cout , g, dp);
 }

Online here

这是基于this answer 在一个类似的问题中,并且编译得很好。

现在我想在一个单独的函数中分离打印,所以我在模板函数中编写相同的代码,只是用模板类型参数替换具体类型:

template<typename graph_t, typename vertex_t>
void RenderGraph( const graph_t& g )
{
    boost::dynamic_properties dp;
    dp.property( "color",   boost::get( &vertex_t::color,    g ) );
    dp.property( "node_id", boost::get( boost::vertex_index, g ) );
    boost::write_graphviz_dp( std::cout, g, dp );
}

int main()
{
    myGraph_t g;
    boost::add_edge(0, 1, g);

    RenderGraph<myGraph_t,myVertex_t>( g );
}

但是这个does not compile

property_map.hpp:361:44:错误:分配只读位置...

任何想法我做错了什么?

【问题讨论】:

    标签: c++ boost graphviz boost-graph


    【解决方案1】:

    property_map.hpp:361:44:错误:分配只读位置...

    是的,可悲的是 g 是 const 的事实使得默认的 property 工厂函数是非法的。如果模型允许,动态属性以可写方式构建:

    要求:PropertyMap 必须对 Readable Property Map 或 Read/Write Property Map 建模。

    因为属性映射是可写的,所以动态属性也编译写分支。

    您必须将参数作为非常量或手动覆盖底层映射的属性特征(请参阅此处的 cmets (Cut set of a graph, Boost Graph Library) 以获取示例)。

    您可能会考虑将此报告为可用性问题,因为从逻辑上讲,那里的属性应该是 const

    【讨论】:

    • 谢谢,刚刚尝试删除const,确实有效,但确实不合逻辑,因此我什至没有尝试过。我会对此进行调查。
    猜你喜欢
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    相关资源
    最近更新 更多