簡體   English   中英

如何解決打印節點和邊緣增強圖形庫中的此錯誤?

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

我已經編寫了以下 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命名一個類型。 如果你想傳遞一個參數並使用它,你必須命名它:

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

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

請注意我是如何標記成員 function const的,因為我們可以。

或者,您可以將其標記為static成員 function 因為您正在傳遞圖表,無論如何,我們根本不使用MyGraphBuilder的成員。 老實說,感覺printGraph不應該是 class 的成員。


獎金:

請注意,在boost/graph/graph_utility.hpp已經有一個print_graph實用程序。 如何讓它打印你想要的屬性取決於你的圖形的實際類型,所以這是一個非常隨機的例子:

住在科利魯

#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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM