std::vector<Node>& Way::GetNodesCollection() const {
return this->nodesCollection;}
这收到错误"类型' std::vector<Node>&
'的引用初始化无效"如何正确初始化它。
您正在尝试从 const
方法返回对类成员的非const
引用。这是不允许的,因为它会违反const
类成员函数的语义(它只能访问 this
的 const 版本)。
要修复您的错误,请编写
const std::vector<Node>& Way::GetNodesCollection() const {
return nodesCollection;
}
或
std::vector<Node>& Way::GetNodesCollection() {
return nodesCollection;
}
正如所写,这不仅仅是一个"getter",因为我可以获取引用然后修改内容。您需要另一个const
修饰符:
const std::vector<Node>& Way::GetNodesCollection() const {
return this->nodesCollection;
}