我试图仅仅返回,而不是改变一个人的价值std::map.它工作,但如果我把它放在const功能上,我应该得到错误No viable overloaded operator[] for type 'const std::map.我的代码如下:
GLuint getCurrentColorAttribute() const {
return m_programs[m_currentActiveProgram].attributes["SourceColor"];
}
Run Code Online (Sandbox Code Playgroud)
这是我的IDE中的错误图像:

该[]运营商std::map是不是const(因为它可以创建一个在地图上一个新的条目,如果不存在),因此你不能从一个调用它const的功能.
您可以at改为使用C++11:
return m_programs.at(m_currentActiveProgram).attribute["SourceColor"];