错误C2678:二进制'!=':找不到运算符

时间:2013-01-12 14:11:32

标签: c++ visual-c++ operator-overloading

完成错误消息:

Error   1   error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'const std::_List_iterator<_Mylist>' (or there is no acceptable conversion)    c:\ebooks\sourcecode\programing_game_ai_by_example\buckland_sourcecode\vs8 projects\buckland_chapter3-steering behaviors\path.h 54  1   Steering

我对[V] C ++世界很新。我正在通过示例书阅读编程游戏AI并尝试研究/分析发布者网站中提供的source code

似乎整个源代码都是用VS8编写的,但我使用的是VS12 Express版。项目转换似乎没有任何问题,我期望为所有项目提供4个警告(见下文)。

但问题是我得到了一个编译错误,在我看来,我的基本知识是强类型列表的运算符重载,但我不确定它是真的我如果它是真的,我不知道如何解决它。

错误#1的代码行:

Vector2D    CurrentWaypoint()const{assert(curWaypoint != NULL); return *curWaypoint;}

问题变量声明:

std::list<Vector2D>::iterator  curWaypoint;

扩展代码段,包括构造函数:

//constructor for creating a path with initial random waypoints. MinX/Y
  //& MaxX/Y define the bounding box of the path.
  Path(int    NumWaypoints,
       double MinX,
       double MinY,
       double MaxX,
       double MaxY,
       bool   looped):m_bLooped(looped)
  {
    CreateRandomPath(NumWaypoints, MinX, MinY, MaxX, MaxY);

    curWaypoint = m_WayPoints.begin();
  }


  //returns the current waypoint
  Vector2D    CurrentWaypoint()const{assert(curWaypoint != NULL); return *curWaypoint;}

  //returns true if the end of the list has been reached
  bool        Finished(){return !(curWaypoint != m_WayPoints.end());}

现在我不确定在此之后要去哪里,但列表和迭代器定义在WindowsUtils.h文件中。

我没有粘贴相同的剩余错误,但是您可以访问整个Path.h文件here

有人能告诉我如何解决这个问题吗?我总是害怕c ++,特别是对于vc ++,但如果有人指导我解决问题的方法和修复方法,我可以自己修复它

错误

Error   1   error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'const std::_List_iterator<_Mylist>' (or there is no acceptable conversion)    c:\ebooks\sourcecode\programing_game_ai_by_example\buckland_sourcecode\vs8 projects\buckland_chapter3-steering behaviors\path.h 54  1   Steering
Error   2   error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'const std::_List_iterator<_Mylist>' (or there is no acceptable conversion)    c:\ebooks\sourcecode\programing_game_ai_by_example\buckland_sourcecode\vs8 projects\buckland_chapter3-steering behaviors\path.h 54  1   Steering
Error   3   error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'const std::_List_iterator<_Mylist>' (or there is no acceptable conversion)    c:\ebooks\sourcecode\programing_game_ai_by_example\buckland_sourcecode\vs8 projects\buckland_chapter3-steering behaviors\path.h 54  1   Steering
Error   4   error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'const std::_List_iterator<_Mylist>' (or there is no acceptable conversion)    c:\ebooks\sourcecode\programing_game_ai_by_example\buckland_sourcecode\vs8 projects\buckland_chapter3-steering behaviors\path.h 54  1   Steering
    5   IntelliSense: no operator "!=" matches these operands
            operand types are: const std::_List_iterator<std::_List_val<std::_List_simple_types<Vector2D>>> != int  c:\eBooks\SourceCode\programing_game_ai_by_example\Buckland_SourceCode\VS8 projects\Buckland_Chapter3-Steering Behaviors\Path.h 54  38  Steering

以防这是否重要,以下是本书源代码中每个项目的警告

* 常见解决方案转换警告:* (注意:与上述项目不同)

Conversion Report - WestWorld1.vcproj: 
Converting project file 'C:\eBooks\SourceCode\programing_game_ai_by_example\Buckland_SourceCode\VS8 projects\Buckland_Chapter2-State Machines\WestWorld1\WestWorld1.vcproj'. 
Web deployment to the local IIS server is no longer supported. The Web Deployment build tool has been removed from your project settings. 
Done converting to new project file 'C:\eBooks\SourceCode\programing_game_ai_by_example\Buckland_SourceCode\VS8 projects\Buckland_Chapter2-State Machines\WestWorld1\WestWorld1.vcxproj'. 
This application has been updated to include settings related to the User Account Control (UAC) feature of Windows Vista. By default, when run on Windows Vista with UAC enabled, this application is marked to run with the same privileges as the process that launched it. This marking also disables the application from running with virtualization. You can change UAC related settings from the Property Pages of the project. 
VCWebServiceProxyGeneratorTool is no longer supported. The tool has been removed from your project settings. 
MSB8012: $(TargetPath) ('C:\eBooks\SourceCode\programing_game_ai_by_example\Buckland_SourceCode\VS8 projects\Buckland_Chapter2-State Machines\WestWorld1\.\Debug\WestWorld1.exe') does not match the Linker's OutputFile property value '.\Debug/WestWorld1.exe' ('C:\eBooks\SourceCode\programing_game_ai_by_example\Buckland_SourceCode\VS8 projects\Buckland_Chapter2-State Machines\WestWorld1\Debug/WestWorld1.exe') in project configuration 'Debug|Win32'. This may cause your project to build incorrectly. To correct this, please make sure that $(TargetPath) property value matches the value specified in %(Link.OutputFile). 
MSB8012: $(TargetPath) ('C:\eBooks\SourceCode\programing_game_ai_by_example\Buckland_SourceCode\VS8 projects\Buckland_Chapter2-State Machines\WestWorld1\.\Release\WestWorld1.exe') does not match the Linker's OutputFile property value '.\Release/WestWorld1.exe' ('C:\eBooks\SourceCode\programing_game_ai_by_example\Buckland_SourceCode\VS8 projects\Buckland_Chapter2-State Machines\WestWorld1\Release/WestWorld1.exe') in project configuration 'Release|Win32'. This may cause your project to build incorrectly. To correct this, please make sure that $(TargetPath) property value matches the value specified in %(Link.OutputFile). 

3 个答案:

答案 0 :(得分:3)

您无法将迭代器与NULL进行比较,这就是您收到这些错误的原因。而且据我所知,除非你把它设置成某种东西,否则没有办法检查是否和迭代器指向某个东西。将它设置为end()将使其成为NULL迭代器(因为end()是容器中最后一个元素之后的元素,因此它基本上没有指向任何内容。)

答案 1 :(得分:0)

iteratorNULL进行比较无效:

assert(curWaypoint != NULL);

我没有查看您的所有代码,但也许您的意思是将其与end()进行比较:

assert(curWaypoint != m_WayPoints.end());

您在构造函数中初始化curWaypoint,因此这应该按照您的意图运行。

答案 2 :(得分:0)

原始源代码的副本与您的有点不同......

  //returns the current waypoint
  Vector2D    CurrentWaypoint()const{assert(*curWaypoint != NULL); return *curWaypoint;}

TBL:在我所拥有的代码副本中,它不是测试迭代器,而是测试迭代器指向的内容。不幸的是,我遇到了同样的错误。