我在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);

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

最佳答案

您无法提供一个独立的示例。从我所看到的东西中拼凑出最低限度,没有问题:

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 -->

关于c++ - 导致 “no function to call error”的add_edge,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49619488/

10-09 10:04