尝试在地图中查找元素时出错

时间:2013-09-27 05:36:16

标签: c++

我正在使用以下代码

void SomeClass::Update(const std::string& symbol, const int& volume)
{
  std::map<std::string,int>& Refcontainer = BTracker;

  //The two conditionals below seem to cause a problem here

  if(Refcontainer.find(symbol) != Refcontainer.end())
  {
    Refcontainer[symbol] = Refcontainer[symbol] + volume;
  }
  else
  {
    Refcontainer[symbol] = 0 + volume;
  }
}

我得到的错误是:

Error   16  error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   10  error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string'   c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   11  error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string'   c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   12  error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string'   c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   1   error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'    c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   2   error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'    c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   3   error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'    c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   13  error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string' c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   14  error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string' c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   15  error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string' c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   7   error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string' c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   8   error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string' c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   9   error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string' c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   4   error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const std::string'   c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   5   error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const std::string'   c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "
Error   6   error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const std::string'   c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional  125 1   " "

有关我为什么会收到此错误以及如何解决此问题的任何建议?

2 个答案:

答案 0 :(得分:4)

你可能忘了#include <string>

VC ++的其他一些标题(iostream?)包含了足够的std::string定义,可以让你定义一个std::string对象,但是当你尝试做很多任何事情。

答案 1 :(得分:4)

我应该提一下,你的代码可以大大简化为

Refcontainer[symbol] += value;

而不是所有条件。