参数 1 从 'Graph<int>*' 到 'Graph<int>&' 没有已知的转换

no known conversion for argument 1 from ‘Graph<int>*’ to ‘Graph<int>&’

本文关键字:lt gt Graph int 转换 参数      更新时间:2023-10-16

我不明白我现在面临的问题:

class Dijkstra {
public:
  Dijkstra(Graph<T> &graph, bool verbose = false)
    :m_graph(graph), m_verbose(verbose){ }  
    [ .. ]
}
Graph<int> *custom = Graph<int>::custom((int *) &nodes[0], 4, 5);
Dijkstra<int> spt(custom, true);

Dijkstra构造函数不接受引用吗?如果是,为什么编译器会抱怨?

graph.cpp:222:37: error: no matching function for call to ‘Dijkstra<int>::Dijkstra(Graph<int>*&, bool)’
       Dijkstra<int> spt(custom, true);
                                     ^
graph.cpp:222:37: note: candidates are:
graph.cpp:128:3: note: Dijkstra<T>::Dijkstra(Graph<T>&, bool) [with T = int]
   Dijkstra(Graph<T> &graph, bool verbose = false)
   ^
graph.cpp:128:3: note:   no known conversion for argument 1 from ‘Graph<int>*’ to ‘Graph<int>&’

graph.cpp:126:7: note: Dijkstra::Dijkstra(const Dijkstra&)类Dijkstra {

我感觉我做错了,所有的事情。

指针和引用是两个不同的东西,在强类型语言中并不总是兼容的。你应该看一下医生了解更多的信息。无论如何,这里有一个解决您的情况的方法:

Graph<int> *custom = Graph<int>::custom((int *) &nodes[0], 4, 5);
Dijkstra<int> spt(&custom, true);

添加,在ref前面返回对象的地址,也是一个指针。