【发布时间】:2015-05-02 20:59:46
【问题描述】:
我目前正在开发一个将 dijkstra 算法与图形结合使用的程序。我得到了一个函数,该函数应该获取在 Graph 类中定义的指定顶点的相邻顶点:
template<class VertexType>
void Graph<VertexType>::GetToVertices(VertexType vertex, Queue<VertexType>& adjvertexQ) const
{
int fromIndex;
int toIndex;
fromIndex = IndexIs(vertex);
for (toIndex = 0; toIndex < numVertices; toIndex++)
if (edges[fromIndex][toIndex] != NULL_EDGE)
adjvertexQ.enqueue(vertices[toIndex]);
}
我正在尝试在我的客户端文件dijkstra.cpp 中使用此功能,如下所示:
void assignWeights(Graph<string> &dGraph, int numVertices, VertexType myVertices[], int startingLocation, Queue<string>& getTo)
{
int currV = startingLocation;
dGraph.GetToVertices(myVertices[startingLocation],adjvertexQ);
}
变量myVertices 是在main 中定义的结构数组,包含有关每个顶点的信息,类型为VertexType,adjvertexQ 是VertexType 对象的队列,用于跟踪相邻顶点。
给出的错误:
dijkstra.cpp: error: no matching function for call to ‘Graph<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::GetToVertices(VertexType&, Queue<VertexType>&)’
graph.cpp: note: candidates are: void Graph<VertexType>::GetToVertices(VertexType, Queue<VertexType>&) const [with VertexType = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]
问题似乎是我通过引用传递了VertexType 变量,但即使我在同一方法中使用临时值,它仍将参数识别为通过引用传递值。知道什么可以解决这个问题吗?
【问题讨论】:
-
对不起,我应该详细说明
VertexType。它是一个包含以下内容的结构:string name用于顶点名称,bool marked,用于检查该顶点是否已被检查,int distance,用于检查此顶点与前一个顶点和@987654335 的距离@ 表示上一个顶点。 -
我将下面的(现已删除)评论扩展为答案。 (知道 VertexType 的内容很好,但不会改变它。)
标签: c++ graph compiler-errors pass-by-reference dijkstra