我无法理解我在这里遇到的问题:
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:注意:Dijkstra :: Dijkstra(const Dijkstra&amp;) Dijkstra等级{
我觉得我错了,所有这一切。
答案 0 :(得分:1)
指针和引用是两个不同的东西,并且在强类型语言中,并不总是兼容。您应该查看文档以获取更多信息。无论如何,这是你的案例的解决方案:
Graph<int> *custom = Graph<int>::custom((int *) &nodes[0], 4, 5);
Dijkstra<int> spt(&custom, true);
添加&amp;在ref前面返回对象的地址,因此是一个指针。