&',c++11,constants"/>

首页 > 解决方案 > '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 上正常编译。

标签: c++11constants

解决方案


const是在错误的地方。应该是const std::vector<size_t>& elementIds
这意味着该功能不允许更改elementIds

同样的情况也是如此map
它应该是const std::map<long unsigned int, long unsigned int>& mapElementIdToGlobalIndex

放置在constOP 中的位置将参考标记为const. 由于无论如何都无法更改引用,因此无需这样做。


推荐阅读