c++11 - 'const' 限定符不能应用于 'std::vector&'
问题描述
我在 linux 上有一个 c++11 项目,我使用了以下签名,该签名无法在 linux 上编译,但可以在 windows 上编译
错误:
error: 'const' qualifiers cannot be applied to 'std::vector<long unsigned int>&'
error: 'const' qualifiers cannot be applied to 'std::map<long unsigned int, long unsigned int>&'
功能是
bool debugGlobalDs(std::vector<size_t> & const elementIds ,
std::map<long unsigned int, long unsigned int>& const mapElementIdToGlobalIndex)
{
....
return true
}
为什么我不能在这里使用 const 限定符?一旦我删除它,它也可以在 Linux 上正常编译。
解决方案
const
是在错误的地方。应该是const std::vector<size_t>& elementIds
。
这意味着该功能不允许更改elementIds
。
同样的情况也是如此map
。
它应该是const std::map<long unsigned int, long unsigned int>& mapElementIdToGlobalIndex
放置在const
OP 中的位置将参考标记为const
. 由于无论如何都无法更改引用,因此无需这样做。
推荐阅读
- excel - 当单元格在该列的同一行中更改时,如何在特定列中输入日期/时间戳?
- java - 这个 KOTLIN 代码的 java 等价物是什么?
- go - 如何使用软删除设置关联 deletedAt 字段?
- python - 使用 Python 将 Json 转换为数据框
- assembly - 如何使用 DOSBox 中的汇编程序消除字符串中的空格和标点符号?
- javascript - 如何检查快速验证器中的字段是否唯一?
- python - 如何防止重复信息进入mysql数据库?
- c++ - 未找到 OpenEXR 头文件
- tcl - TCL 尾文件名
- c++ - 如何使用模板化的 constexpr 成员函数初始化 constexpr std::array?