add_edge导致“无函数调用错误”

时间:2018-04-02 22:09:23

标签: c++ boost graph edges

我的add_edge函数上有“无法调用函数”。 Boost肯定包含正确,所以我不认为这是问题所在。这是它所调用的函数:

void initializeGraph(Graph &g,
                  Graph::vertex_descriptor &start,
                 Graph::vertex_descriptor &end, ifstream &fin)
// Initialize g using data from fin.  Set start and end equal
// to the start and end nodes.
{
edgeProperties e;

int n, i, j;
int startId, endId;
fin >> n;
fin >> startId >> endId;
Graph::vertex_descriptor v;

// Add nodes.
for (int i = 0; i < n; i++)
{
    v = add_vertex(g);
    if (i == startId)
        start = v;
    if (i == endId)
        end = v;
}

while (fin.peek() != '.')
{
    fin >> i >> j >> e.weight;
    add_edge(i,j,e,g);
}
}

以下是我调用函数的方法:

Graph g;
Graph::vertex_descriptor start, end, curr;
initializeGraph(g, start, end, infile);

关于为什么会发生这种情况的任何想法都会很棒,因为我真的输了!

1 个答案:

答案 0 :(得分:0)

您无法提供自包含的示例。从我看到的东西拼凑出最小的东西,没有问题:

<强> Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <fstream>

struct edgeProperties {
    double weight;
};

using Graph = boost::adjacency_list<boost::vecS, boost::vecS,boost::directedS, boost::no_property, edgeProperties>;

void initializeGraph(Graph &g, Graph::vertex_descriptor &start, Graph::vertex_descriptor &end, std::ifstream &fin)
// Initialize g using data from fin.  Set start and end equal to the start and end nodes.
{
    edgeProperties e;

    int n, i, j;
    int startId, endId;
    fin >> n;
    fin >> startId >> endId;
    Graph::vertex_descriptor v;

    // Add nodes.
    for (int i = 0; i < n; i++) {
        v = add_vertex(g);
        if (i == startId)
            start = v;
        if (i == endId)
            end = v;
    }

    while (fin.peek() != '.' && fin >> i >> j >> e.weight) {
        add_edge(i, j, e, g);
    }
}

#include <boost/graph/graph_utility.hpp>

int main() {
    std::ifstream infile("input.txt");
    Graph g;
    Graph::vertex_descriptor start, end/*, curr*/;
    initializeGraph(g, start, end, infile);

    print_graph(g);
}

对于input.txt:

5
0 4
2 3 1
1 2 1
3 4 1
0 2 1
.

打印:

0 --> 2 
1 --> 2 
2 --> 3 
3 --> 4 
4 -->