我想通过引用将对象传递给Contructor,但遇到问题,因为我不知道如何将其绑定到类的变量。
在这里,我发布了一些代码,并且出现了错误。
class ShortestPath{
public:
ShortestPath(Graph& graph): graph(graph){};[...]
private:
Graph *graph;
};
在这种情况下,错误上升为:
cannot convert `Graph' to `Graph*' in initialization
如果我写
ShortestPath(Graph& graph): *graph(graph){};[...]
错误是
expected identifier before '*' token
当我调用构造函数时,应该这样调用吗?ShortestPath(Graph);
您必须按以下方式更改代码:
class ShortestPath{
public:
ShortestPath(Graph& graph): graph(graph){};[...]
private:
Graph &graph;
}
要么:
class ShortestPath{
public:
ShortestPath(Graph& graph): graph(&graph){};[...]
private:
Graph *graph;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句