gpt4 book ai didi

c++ - C++没有匹配函数可解决调用错误(默认通过引用传递)

转载 作者:行者123 更新时间:2023-12-02 11:02:57 25 4
gpt4 key购买 nike

我目前正在开发一个使用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,而 adjvertexQVertexType对象队列,用于跟踪相邻顶点。

给出错误:
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变量,但是即使在同一方法中使用临时值时,它仍然会将参数识别为通过引用值传递。有什么想法可以解决这个问题吗?

最佳答案

我假设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<VertexType>有一个方法
GetToVertices(VertexType vertex, Queue<VertexType>& adjvertexQ) const  

但是像 Graph<string>中用作参数的 assignWeights有一种方法
GetToVertices(string vertex, Queue<string>& adjvertexQ) const  

...

因此,在 assignWeights中,您有一个带有字符串的 Graph<string>GetToVertices
但是您要传递VertexType类的变量。

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

关于c++ - C++没有匹配函数可解决调用错误(默认通过引用传递),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30007432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com