c++ - boost::graph add_vertex 编译错误

标签 c++ boost-graph

我正在尝试使用 boost::graph header 库,但我仍然无法将 Verticle 添加到我的图表中。

下面是我如何使用 add_vertex 函数:

void GraphManager::addToGraph(VertexProperties node){

    //no error, but i do not need it
    vertex_t v = boost::add_vertex(graph);

    //compilation error
    vertex_t v = boost::add_vertex(node, graph);

    /*...*/
}

我的定义在这里:

#ifndef GRAPH_DEFINITION_H
#define GRAPH_DEFINITION_H

#include <boost/graph/adjacency_list.hpp>
#include "matchedword.h"

typedef MatchedWord* VertexProperties;

struct EdgeProperties
{
   int distance;
   EdgeProperties() : distance(0) {}
   EdgeProperties(int d) : distance(d) {}
};

struct GraphProperties {

};

typedef boost::adjacency_list<
   boost::vecS, boost::vecS, boost::undirectedS,
   boost::property<VertexProperties, boost::vertex_bundle_t>,
   boost::property<EdgeProperties, boost::edge_bundle_t>,
   boost::property<GraphProperties, boost::graph_bundle_t>
> Graph;

typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
typedef boost::graph_traits<Graph>::edge_descriptor edge_t;


#endif // GRAPH_DEFINITION_H

有什么想法吗? 谢谢。

error: no matching function for call to 'add_vertex(MatchedWord*&, Graph&)' candidates are: [...] template typename Config::vertex_descriptor boost::add_vertex(const typename Config::vertex_property_type&, boost::adj_list_impl&)

note: template argument deduction/substitution failed:

note: 'Graph {aka boost::adjacency_list, boost::property, boost::property >}' is not derived from 'boost::adj_list_impl'

我不明白这个错误输出的意思。

最佳答案

使用捆绑属性更容易。

像这样:

// A class to hold bundled properties for a vertex
// In this case, we have a pointer to a MatchedWord object

class vertex_props {
public:
MatchedWord * pMW;
};

定义具有捆绑属性的图形类型

typedef boost::adjacency_list<
boost::vecS, boost::vecS, boost::undirectedS,
vertex_props,
...
> Graph_t;

现在您可以像这样添加具有指定捆绑属性的顶点

void GraphManager::addToGraph(MatchedWord * node){
   graph[ boost::add_vertex(graph) ].pMW = node;
   ...

关于c++ - boost::graph add_vertex 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17595949/

相关文章:

c++ - Fruchterman Reingold 布局不收敛

c++ - adjacency_list 与 VertexList 不同于 vecS

c++ - 如何在 C++ 中同时转换右值和左值参数?

c++ - 将 Int32 NAN 转换为 float

c++ - 为什么 VC C4244 警告(可能丢失数据)专门处理 'int'?

c++ - 使用迭代器打印 multimap

c++ - 是否可以设置 std::tr1::tuple 的默认值?

c++ - 如何使用属性包将边添加到 boost::adjacency_matrix?

c++ - Boost graphviz自定义顶点标签

c++ - 如何在boost中遍历图使用BFS