将C++代码从 VS2003 迁移到 VS2010 后出现错误 C2678,错误 C2679:二进制'=':未找到采用类型为 'int' 的右侧操作数的运算符

Error C2678 after migrating C++ code from VS2003 to VS2010, error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int'

本文关键字:错误 类型 操作数 运算符 int 二进制 VS2003 迁移 代码 C++ VS2010      更新时间:2023-10-16
std::vector<std::string>::iterator it;
it = NULL;
do
{
    if(it == NULL)
        it = init.begin();
    else
        ++it;
    if(it == init.end())
        return 1; 
}
while(it->empty());

上面的代码在VS2003中工作得很好,但是当它迁移到VS2010时,它会给出编译错误,说

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
1>          c:program filesmicrosoft visual studio 10.0vcincludevector(388): could be 'std::_Vector_iterator<_Myvec> &std::_Vector_iterator<_Myvec>::operator =(const std::_Vector_iterator<_Myvec> &)'
1>          with
1>          [
1>              _Myvec=std::_Vector_val<std::string,std::allocator<std::string>>
1>          ]
1>          while trying to match the argument list '(std::_Vector_iterator<_Myvec>, int)'
1>          with
1>          [
1>              _Myvec=std::_Vector_val<std::string,std::allocator<std::string>>
1>          ]
1>d:vs2010_wsacct ford 6.2.4 source codeacct_ford_wsvectorsrcdrivercancardxl.cpp(81): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::_Vector_iterator<_Myvec>' (or there is no acceptable conversion)
1>          with
1>          [
1>              _Myvec=std::_Vector_val<std::string,std::allocator<std::string>>
1>          ]

您假设vector迭代器可以从int类型(或某种形式的指针?)进行初始化和赋值。事实并非如此。

你可以把你的循环转换成:

if (init.empty())
  return 1;
it = init.begin();
while (it->empty())
{
  ++it;
  if (it == init.end())
    return 1;
}

这是因为你试图将NULL赋值给迭代器(它不是指针!)编译器明确表示不支持此操作。顺便问一下,这样的任务是什么原因?

通常情况下,如果你想运行集合,你应该这样写:

for (VectorType::iterator it = init.begin(); it != init.end(); ++it)
{
     // do something with it here   
}