1

我正在使用以下代码

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   " "

关于为什么我收到此错误以及如何解决它的任何建议?

4

2 回答 2

4

你可能已经忘记了#include <string>

一些 VC++ 的其他头文件(iostream?)包含足够的定义std::string来让你定义一个std::string对象,但是当你尝试用它做很多事情时会失败并出现类似这些奇怪的错误。

于 2013-09-27T05:39:03.290 回答
4

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

Refcontainer[symbol] += value;

而是所有条件。

于 2013-09-27T05:44:51.420 回答