简体   繁体   English

如何解决打印节点和边缘增强图形库中的此错误?

[英]How can I solve this error in Printing Nodes and Edges Boost Graph Library?

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'我已经编写了以下 function 来打印出我的图形节点和边的大小,但是这个 function 不起作用,错误是:错误:非静态数据成员“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 names a type. MyGraphBuilder::MyGraph命名一个类型。 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";
}

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 :为了提高效率,您可以通过引用传递(在这种情况下, const&将起作用,因为您没有修改图形)以避免复制整个图形只是为了调用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";
}

Note how I marked the member function const as well, because we can.请注意我是如何标记成员 function const的,因为我们可以。

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.或者,您可以将其标记为static成员 function 因为您正在传递图表,无论如何,我们根本不使用MyGraphBuilder的成员。 Honestly, that feels like printGraph should not be a member of that class in the first place.老实说,感觉printGraph不应该是 class 的成员。


Bonus:奖金:

Be advised that there is a print_graph utility in boost/graph/graph_utility.hpp already.请注意,在boost/graph/graph_utility.hpp已经有一个print_graph实用程序。 How to make it print the properties you want depends on the actual type of your graph, so here's a pretty random example:如何让它打印你想要的属性取决于你的图形的实际类型,所以这是一个非常随机的例子:

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

Printing:印刷:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM