【问题标题】:C++ no matching function for call error (defaulting to pass by reference)C++没有匹配函数调用错误(默认通过引用传递)
【发布时间】: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 中定义的结构数组,包含有关每个顶点的信息,类型为VertexTypeadjvertexQVertexType 对象的队列,用于跟踪相邻顶点。

给出的错误:

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


【解决方案1】:

我假设 assignWeights 不是 Graph 类的一部分。

通过引用/值/任何东西传递都不是问题,
但是您混淆了不同的 VertexType。

a) 你有一个函数

void assignWeights(Graph<string> &dGraph, int numVertices, VertexType myVertices[], int startingLocation, Queue<string>& getTo) 

其中VertexType 是其他地方的类、结构或typedef。

b) 你有一个类方法

template<class VertexType>
void Graph<VertexType>::GetToVertices(VertexType vertex, Queue<VertexType>& adjvertexQ) const 

其中VertexType 是一个模板类型。这意味着,Graph&lt;VertexType&gt; 有一个方法

GetToVertices(VertexType vertex, Queue<VertexType>& adjvertexQ) const  

但是在assignWeights 中用作参数的Graph&lt;string&gt; 有一个方法

GetToVertices(string vertex, Queue<string>& adjvertexQ) const  

...

所以,在assignWeights 中,您有一个Graph&lt;string&gt; 和一个GetToVertices 想要字符串,
但是您正在传递 VertexType 类的变量。

复制的代码与您的程序构建方式不兼容
(或者你对自己的代码感到困惑)

【讨论】:

  • 这似乎是问题谢谢。只是为了直接得到答案:因为在我的实现文件中的函数GetToVertices 中定义了一个模板,我的客户端文件assignWeights 中的函数有一个使用字符串的图表,我的实现中的函数更改以适应参数能被assignWeights的人通过吗?
  • GetToVertices 不适应assignWeights,而是适应Graph&lt;string&gt; 的字符串(或&lt;&gt; 之间的任何内容)。 Graph中的VertexType只是&lt;&gt;之间的东西的占位符,与你的结构无关。但是assignWeights 正在使用该结构,并试图将其提供给Graph&lt;string&gt; 的一部分。 ...
猜你喜欢
  • 2011-05-16
  • 2014-10-11
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多