如何解决“打印节点和边缘Boost Graph库"中的此错误? [英] How can I solve this error in Printing Nodes and Edges Boost Graph Library?

查看:81
本文介绍了如何解决“打印节点和边缘Boost Graph库"中的此错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下函数以仅打印我的图形节点"和边"的大小,但是此函数不起作用,错误为: 错误:无效使用非静态数据成员'MyGraph'

I have written the following function to print out just the size of My Graph Nodes and Edges, but this function didn't work, the Error was : error: invalid use of non-static data member '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命名一种类型.如果要传递参数并使用它,则必须将其命名为:

MyGraphBuilder::MyGraph names a type. If you want to pass a parameter and use it, you'll have to name it:

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";
}

为提高效率,您可以按引用传递(在这种情况下,const&将起作用,因为您没有修改图形),以避免仅出于对printGraph的调用而复制整个图形:

To be more efficient you can pass by reference (in this case, const& will work because you're not modifying the graph) to avoid copying the entire graph just for the call to printGraph:

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";
}

请注意,我也是如何标记成员函数const的,因为我们可以.

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

或者您可以将其标记为static成员函数,因为 无论如何,您正在传递图形,我们没有使用的成员 完全MyGraphBuilder.老实说,感觉像printGraph应该 首先不要成为该类的成员.

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.


奖金:


Bonus:

请注意,boost/graph/graph_utility.hpp中已经有一个print_graph实用程序.如何使其打印所需的属性取决于图形的实际类型,因此,这是一个非常随机的示例:

Be advised that there is a print_graph utility in boost/graph/graph_utility.hpp already. How to make it print the properties you want depends on the actual type of your graph, so here's a pretty random example:

在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

这篇关于如何解决“打印节点和边缘Boost Graph库"中的此错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆