我正在尝试返回一个Vertex &
,代码如下:
Vertex& Graph::getVertex(std::string v) { // gets the vertex
for (std::vector<Vertex>::iterator it = vertices.begin(); it != vertices.end(); it++) {
if ((it->getName()).compare(v) == 0)
return it; // if strings are the same return vertex
}
exit(1);
}
问题是 getVertex
被标记为不兼容,返回中的 it
被标记为 Vertex &
类型的引用(非const qualified) 不能用 std::vector
类型的值初始化 ...
我将如何修复这些错误?
最佳答案
您试图返回迭代器,而不是迭代器指向的内容。所以你需要返回*it
。
关于c++ - 如何通过引用返回 vector 中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18567342/