c++ - 如何解决 Printing Nodes and Edges Boost Graph Library 中的这个错误?

标签 c++ boost graph boost-graph

我编写了以下函数来打印出我的图形节点和边的大小,但是这个函数不起作用,错误是: 错误:无效使用非静态数据成员“MyGraph”

typedef adjacency_list < vecS, vecS, directedS, property < vertex_name_t, idType >, property < edge_weight_t, double > > graph_t;
class MyGraphBuilder{
private:
graph_t MyGraph;
}
void MyGraphBuilder::printGraph(MyGraphBuilder::MyGraph){
unsigned long long NodesCount = num_vertices(MyGraphBuilder::MyGraph);
cout<<"Number of Vertices is :\t"<<NodesCount<<"\n";
unsigned long long EdgesCount = num_edges(MyGraphBuilder::MyGraph);
cout<<"Number of Vertices is :\t"<<EdgesCount<<"\n";
}

最佳答案

MyGraphBuilder::MyGraph 命名一个类型。如果你想传递一个参数并使用它,你必须给它命名:

void MyGraphBuilder::printGraph(MyGraphBuilder::MyGraph g) {
    cout << "Number of Vertices is :\t" << num_vertices(g) << "\n";
    cout << "Number of Edges is :\t" << num_edges(g) << "\n";
}

为了 boost 效率,您可以通过引用传递(在这种情况下,const& 将起作用,因为您没有修改图形)以避免仅为调用 复制整个图形打印图:

void MyGraphBuilder::printGraph(MyGraphBuilder::MyGraph const& g) const {
    cout << "Number of Vertices is :\t" << num_vertices(g) << "\n";
    cout << "Number of Edges is :\t" << num_edges(g) << "\n";
}

Note how I marked the member function const as well, because we can.

Alternatively you can mark is as a static member function because you're passing the graph in, anyways, we are not using members of MyGraphBuilder at all. Honestly, that feels like printGraph should not be a member of that class in the first place.


奖励:

请注意,boost/graph/graph_utility.hpp 中已经有一个 print_graph 实用程序。如何让它打印出你想要的属性取决于你的图表的实际类型,所以这是一个非常随机的例子:

Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
#include <iostream>
#include <random>

using boost::make_iterator_range;

struct VertexProperties { std::string name; };
struct EdgeProperties { double weight = 0; };

struct MyGraphBuilder {
    using MyGraph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, VertexProperties, EdgeProperties>;

    MyGraphBuilder(MyGraph& g) : _target(g) {}
    void generate();

  private:
    MyGraph& _target;
};

void printGraph(MyGraphBuilder::MyGraph const& g) {
    std::cout << "Number of Vertices is:" << num_vertices(g) << "\n";
    std::cout << "Number of Edges is:" << num_edges(g) << "\n";

    boost::print_graph(g, boost::get(&VertexProperties::name, g), std::cout);

    // to print with edge weights:
    for (auto v : make_iterator_range(vertices(g))) {
        for (auto oe : make_iterator_range(out_edges(v, g))) {
            std::cout << "Edge " << oe << " weight " << g[oe].weight << "\n";
        }
    }
}

#include <boost/graph/random.hpp>
void MyGraphBuilder::generate() {
    std::mt19937 prng { 42 }; // fixed random seed
    boost::generate_random_graph(_target, 5, 5, prng);

    _target[0].name = "one";
    _target[1].name = "two";
    _target[2].name = "three";
    _target[3].name = "four";
    _target[4].name = "five";

    for (auto e : boost::make_iterator_range(edges(_target))) {
        _target[e].weight = std::uniform_real_distribution<>(1.0, 10.0)(prng);
    }
}

int main() {
    MyGraphBuilder::MyGraph g;

    {
        MyGraphBuilder builder{g};
        builder.generate();
    }

    printGraph(g);
}

打印:

Number of Vertices is:5
Number of Edges is:5
one --> 
two --> four 
three --> one one 
four --> three 
five --> one 
Edge (1,3) weight 1.52275
Edge (2,0) weight 8.79559
Edge (2,0) weight 6.41004
Edge (3,2) weight 7.37265
Edge (4,0) weight 1.18526

关于c++ - 如何解决 Printing Nodes and Edges Boost Graph Library 中的这个错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58974799/

相关文章:

C++: boost ptree相对键

c++ - VS2013 使用 NuGet boost

Java实现加权图?

c# - QuickGraph 3.6 中的 .NET 二进制序列化

javascript - 谷歌图表 : Line graph + points?

c++ - 如何在Qt中映射资源文件?

c++ - GDB:警告:在重载方法上设置了多个断点

c++ - 无法从闭包内传递 `std::array`

c++ - 从 std::string 获取类型,C++

c++ - 如何解析 POST body/GET 参数?