【问题标题】:I'm trying to use this Boost C++ code and I have a problem我正在尝试使用此 Boost C++ 代码,但我遇到了问题
【发布时间】:2011-02-09 01:44:46
【问题描述】:

首先我会解释,然后我会粘贴代码。我实际上从这个例子中复制了代码 http://www.boost.org/doc/libs/1_45_0/libs/graph/example/prim-example.cpp 然后我试图让它与来自文本文件的输入一起工作,就像我为 Boost Kruskal 算法所做的那样。

使用调试器,我知道这个函数的第二个参数想要“结束”边数组。这就是我在函数调用中给出的,我不明白。

Graph g(edges, edge_array + num_edges, weights, num_nodes);

我收到此错误

1>c:\users\edmond\documents\visual studio 2008\projects\boost prim algo\boost prim algo\main.cpp(61) : error C2661: 'boost::adjacency_list<OutEdgeListS,VertexListS,DirectedS,VertexProperty,EdgeProperty>::adjacency_list' : no overloaded function takes 4 arguments
1>        with
1>        [
1>            OutEdgeListS=boost::vecS,
1>            VertexListS=boost::vecS,
1>            DirectedS=boost::undirectedS,
1>            VertexProperty=boost::property<boost::vertex_distance_t,int>,
1>            EdgeProperty=boost::property<boost::edge_weight_t,int>
1>        ]

这是完整的代码。原代码我已经注释了,但是你也可以在我给的网站上找到原代码。

//=======================================================================
// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, 
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
#include <boost/config.hpp>
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/prim_minimum_spanning_tree.hpp>

int
main()
{
  using namespace boost;
  typedef adjacency_list < vecS, vecS, undirectedS,
    property<vertex_distance_t, int>, property < edge_weight_t, int > > Graph;
  typedef std::pair < int, int >E;

  //const int num_nodes = 5;
  //E edges[] = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3),
  //  E(3, 4), E(4, 0)
  //};
  //int weights[] = { 1, 1, 2, 7, 3, 1, 1 };
  //int num_edges = 7;

//Lire un fichier contenant les 2 structures de données
int num_nodes = 0;
std::size_t num_edges = 0;
int * weights;
E * edge_array;
static char ligne[50];  //Ligne lue
bool premiereLignefaite = false;
FILE* fichier = fopen("graph_poids.txt", "r");
int i = 0;

while (fgets(ligne, 50, fichier) != NULL) //retourne 0 quand on a end-of-file
    {
        //La premiere ligne est différente
        if (premiereLignefaite == false) {
            //Initialiser une matrice d'adjacence NxN
            sscanf(ligne, "%d %d", &num_nodes, &num_edges );
            edge_array = new E[num_edges];
            weights = new int[num_edges];
            premiereLignefaite = true;
            continue;
        }
        //On construit notre liste d'arêtes
        int sommet1, sommet2, poids;
        sscanf(ligne, "%d %d %d", &sommet1, &sommet2, &poids);
        weights[i] = poids;
        edge_array[i].first = sommet1;
        edge_array[i].second = sommet2;
        i++;
    }

E* machin = edge_array + num_edges; //aller au dernier élément

  Graph g(edges, edge_array + num_edges, weights, num_nodes);
//Graph g(edges, edges + sizeof(edges) / sizeof(E), weights, num_nodes);
  property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, g);

  std::vector < graph_traits < Graph >::vertex_descriptor >
    p(num_vertices(g));

  prim_minimum_spanning_tree(g, &p[0]);

  for (std::size_t i = 0; i != p.size(); ++i)
    if (p[i] != i)
      std::cout << "parent[" << i << "] = " << p[i] << std::endl;
    else
      std::cout << "parent[" << i << "] = no parent" << std::endl;

  return EXIT_SUCCESS;
}

如果您想尝试一下,这里有一些测试数据。文件名是graph_poids.txt

14 19 
0 2 4
0 4 9
0 1 7
1 6 2
2 3 6
3 5 7
3 4 4
4 13 9
5 7 7
5 6 6
6 8 9
6 10 4
7 9 4
7 8 7
8 11 7
8 10 7
9 12 7
9 11 10
12 13 5

【问题讨论】:

  • 它应该是“edge_array”而不是“edges”(那是原始代码的一部分)。 Ca devrait marcher par la suite!
  • Mikael,如果你发帖,我会给你接受的答案。你让我的代码工作,我没有注意到这个错误,现在它工作正常。
  • @Mikael Persson 如果你没有看到上面针对你的评论......

标签: c++ templates boost


【解决方案1】:

它应该是“edge_array”而不是“edges”(那是原始代码的一部分)。

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 2022-07-12
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    相关资源
    最近更新 更多