1、问题描述

有这样一个函数

void setInfo(const std::map<std::string,std::string>& config)
{
  std::string a = config["aaa"];
}

2、编译现象

如标题

3、解释

config变量是一个const的,但是在内部函数里调用了这个变量的符号重载函数"[ ]",而我们规定const是不能被修改,但是调用这个函数就有被修改的风险,故编译报错

4、解决

可以在函数后面加一个const,或者把config前面的const去掉

相关文章:

  • 2021-08-01
  • 2022-12-23
  • 2022-12-23
  • 2021-07-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-12
  • 2021-06-13
  • 2021-08-28
相关资源
相似解决方案