0
std::vector<Node>& Way::GetNodesCollection() const {
return this->nodesCollection;}
这是得到错误 如何正确初始化该“类型‘std::vector<Node>&
’的引用无效初始化”。吸气剂参考类型变量C++
std::vector<Node>& Way::GetNodesCollection() const {
return this->nodesCollection;}
这是得到错误 如何正确初始化该“类型‘std::vector<Node>&
’的引用无效初始化”。吸气剂参考类型变量C++
您正试图从const
方法返回非const
对类成员的引用。这是不允许的,因为它违反了const
类成员函数的语义(它只能访问常量版本this
)。
要改正错误,要么写
const std::vector<Node>& Way::GetNodesCollection() const {
return nodesCollection;
}
或
std::vector<Node>& Way::GetNodesCollection() {
return nodesCollection;
}
书面,这不只是一个“吸气剂”,因为我可以得到引用,然后修改内容。你想另一个const
修改:
const std::vector<Node>& Way::GetNodesCollection() const {
return this->nodesCollection;
}
要么删除'const'预选赛或返回一个const引用('标准::矢量常量&')。 –
lisyarus