映射运算符 [] 操作数
大家好,我在成员函数中有以下内容
int tt = 6;
vector<set<int>>& temp = m_egressCandidatesByDestAndOtMode[tt];
set<int>& egressCandidateStops = temp.at(dest);
以及成员变量的以下声明
map<int, vector<set<int>>> m_egressCandidatesByDestAndOtMode;
但是我在编译时收到错误(英特尔编译器 11.0)
1>C:\projects\svn\bdk\Source\ZenithAssignment\src\Iteration\PtBranchAndBoundIterationOriginRunner.cpp(85): error: no operator "[]" matches these operands
1> operand types are: const std::map<int, std::vector<std::set<int, std::less<int>, std::allocator<int>>, std::allocator<std::set<int, std::less<int>, std::allocator<int>>>>, std::less<int>, std::allocator<std::pair<const int, std::vector<std::set<int, std::less<int>, std::allocator<int>>, std::allocator<std::set<int, std::less<int>, std::allocator<int>>>>>>> [ const int ]
1> vector<set<int>>& temp = m_egressCandidatesByDestAndOtMode[tt];
1> ^
我知道这一定是一些愚蠢的事情,但我看不出我做错了什么。
更新 我从 const 成员函数调用它,这就是成员变量的类型是 const 的原因,所以我认为类似以下的内容应该修复它:
int dest = 0, tt = 6;
const set<int>& egressCandidateStops = m_egressCandidatesByDestAndOtMode[tt].at(dest);
但是没有骰子...仍然是相同的错误。
Hi all I have the following in a member function
int tt = 6;
vector<set<int>>& temp = m_egressCandidatesByDestAndOtMode[tt];
set<int>& egressCandidateStops = temp.at(dest);
and the following declaration of a member variable
map<int, vector<set<int>>> m_egressCandidatesByDestAndOtMode;
However I get an error when compiling (Intel Compiler 11.0)
1>C:\projects\svn\bdk\Source\ZenithAssignment\src\Iteration\PtBranchAndBoundIterationOriginRunner.cpp(85): error: no operator "[]" matches these operands
1> operand types are: const std::map<int, std::vector<std::set<int, std::less<int>, std::allocator<int>>, std::allocator<std::set<int, std::less<int>, std::allocator<int>>>>, std::less<int>, std::allocator<std::pair<const int, std::vector<std::set<int, std::less<int>, std::allocator<int>>, std::allocator<std::set<int, std::less<int>, std::allocator<int>>>>>>> [ const int ]
1> vector<set<int>>& temp = m_egressCandidatesByDestAndOtMode[tt];
1> ^
I know it's got to be something silly but I can't see what I've done wrong.
UPDATE I'm calling this from a const member function which is why the member variable's type is const so I thought that something like the following should fix it:
int dest = 0, tt = 6;
const set<int>& egressCandidateStops = m_egressCandidatesByDestAndOtMode[tt].at(dest);
But no dice... still the same error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
map::operator[]
不适用于const map
。我回答了 这是几天前的事。
map::operator[]
does not work with aconst map
.I answered this a few days ago.
[]
的原型是一个非常量操作,因此您不能在 const 成员函数的成员上调用它。
您可以将代码更改为:
The prototype for
[]
isi.e. a non const operation, so you can't call it on a member from a const member function .
You could change the code to: