我编写了以下函数以仅打印“我的图形节点”和“边”的大小,但是此函数不起作用,错误为: 错误:无效使用非静态数据成员'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";
}
答案 0 :(得分:1)
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";
}
请注意,我也是如何标记成员函数
const
的原因,您也可以将is标记为
static
成员函数,因为 无论如何,您正在传递图形,我们没有使用 完全MyGraphBuilder
。老实说,感觉printGraph
应该 首先不要成为该类的成员。
奖金:
请注意,print_graph
中已经有一个boost/graph/graph_utility.hpp
实用程序。如何使其打印所需的属性取决于图形的实际类型,因此这是一个非常随机的示例:
#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