将全局常量作为"this"参数传递会丢弃限定符
Passing global constant as 'this' argument discards qualifiers
在我的程序中,我有一个全局const std::map
变量和一个试图读取其内容的函数。编译器(G++5.2.0 C++0x)然后给我一个错误passing ‘const std::map<const char*, const char>’ as ‘this’ argument discards qualifiers [-fpermissive]
。我不能为函数指定cv限定符,因为它不是成员函数。我该怎么办?
代码如下:
const map <const char*, const char> opcodes =
{
{"ABCDE", 123},
};
bool _cmdmatch (const char* cmd, const char* cmdlist[], char count)
{
...
if (opcodes[cmd] == opcodes[cmdlist[i]]) return true;
//if (opcodes.at(cmd) == opcodes.at(cmdlist[i])) return true; //The solution
...
}
我假设您正在使用operator[]
从映射中读取,这在const
映射上是无法使用的。应该使用at()
获取具有特定键的元素,或者使用find()
搜索元素。两者都有const
版本,可以在const
映射上使用。
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- #定义c-预处理器常量..我做错了什么
- 为什么使用 "this" 指针调用派生成员函数?
- 用C++中的一个变量定义一个常量
- 什么时候在C++中返回常量引用是个好主意
- 代理对象的常量正确性
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 通过从构造函数中的'this'复制的指针改变常量对象
- 整体模板参数。错误:在常量表达式中使用'this'
- 初始化列表中常量变量的"this"关键字
- JUCE - 成员函数不可行:'this'参数具有常量类型
- 从 const 成员函数返回 'this' 作为非常量
- 'this'不能用于常量表达式错误 (C++)
- C++static_cast添加常量和"this"对象的const_cast有什么区别?
- 如何将常量函数与"this"一起使用
- 将全局常量作为"this"参数传递会丢弃限定符
- 如果"this"不是常量,为什么我不能修改它?
- 'this'参数具有常量类型,但函数未标记为常量
- 常量不匹配:2 个重载对'this'指针没有合法转换
- 我可以有一个返回*this并处理非常量对象的const成员函数吗