错误 C2582: 'operator ='函数不可用。(xutility)

error C2582: 'operator =' function is unavailable. (xutility)

本文关键字:xutility C2582 operator 错误 函数      更新时间:2023-10-16

我得到一个错误发生在'xutility'类-它是锁定的,因为我没有创建它

error C2582: 'operator =' function is unavailable in 'Agent'

错误指向代码中的以下几行:

    // TEMPLATE FUNCTION move
template<class _InIt,
class _OutIt> inline
_OutIt _Move(_InIt _First, _InIt _Last,
    _OutIt _Dest, _Nonscalar_ptr_iterator_tag)
{   // move [_First, _Last) to [_Dest, ...), arbitrary iterators
for (; _First != _Last; ++_Dest, ++_First)
    *_Dest = _STD move(*_First); // this line has the error
return (_Dest);
}

为什么会发生这种情况?这意味着什么,我该如何解决?

编辑-这是我从输出中抓取的,有人能帮助我理解这一点吗?对不起,我是一个完全的新手…

1>------ Build started: Project: D3D10DEMO, Configuration: Debug Win32 ------
1>  Level.cpp
1>c:usersasherdocumentsmy dropboxdirect3dd3d10demo_1.0d3d10demolevel.cpp(449): warning     C4018: '<' : signed/unsigned mismatch
1>  Brain.cpp
1>c:usersasherdocumentsmy dropboxdirect3dd3d10demo_1.0d3d10demobrain.cpp(43): warning C4413: 'Brain::nodes' : reference member is initialized to a temporary that doesn't persist after the  constructor exits
1>          c:usersasherdocumentsmy dropboxdirect3dd3d10demo_1.0d3d10demobrain.h(34) : see declaration of 'Brain::nodes'
1>c:usersasherdocumentsmy dropboxdirect3dd3d10demo_1.0d3d10demobrain.cpp(43): warning C4413: 'Brain::roomNodeVectors' : reference member is initialized to a temporary that doesn't persist after the constructor exits
1>          c:usersasherdocumentsmy dropboxdirect3dd3d10demo_1.0d3d10demobrain.h(35) : see declaration of 'Brain::roomNodeVectors'
1>c:program files (x86)microsoft visual studio 10.0vcincludexutility(2514): error C2582: 'operator =' function is unavailable in 'Agent'
1>          c:program files (x86)microsoft visual studio 10.0vcincludexutility(2535) : see   reference to function template instantiation '_OutIt std::_Move<_InIt,_OutIt> (_InIt,_InIt,_OutIt,std::_Nonscalar_ptr_iterator_tag)' being compiled
1>          with
1>          [
1>              _OutIt=Agent *,
1>              _InIt=Agent *
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludevector(1170) : see reference to function template instantiation '_OutIt std::_Move<Agent*,Agent*>(_InIt,_InIt,_OutIt)' being compiled
1>          with
1>          [
1>              _OutIt=Agent *,
1>              _InIt=Agent *
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludevector(1165) : while compiling class template member function 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Myvec>)'
1>          with
1>          [
1>              _Myvec=std::_Vector_val<Agent,std::allocator<Agent>>,
1>              _Ty=Agent
1>          ]
1>          c:usersasherdocumentsmy dropboxdirect3dd3d10demo_1.0d3d10demobrain.h(41) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=Agent
1>          ]
1>c:program files (x86)microsoft visual studio 10.0vcincludexutility(2514): error C2582: 'operator =' function is unavailable in 'Pickup'
1>          c:program files (x86)microsoft visual studio 10.0vcincludexutility(2535) : see reference to function template instantiation '_OutIt std::_Move<_InIt,_OutIt>(_InIt,_InIt,_OutIt,std::_Nonscalar_ptr_iterator_tag)' being compiled
1>          with
1>          [
1>              _OutIt=Pickup *,
1>              _InIt=Pickup *
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludevector(1170) : see reference to function template instantiation '_OutIt std::_Move<Pickup*,Pickup*>(_InIt,_InIt,_OutIt)' being compiled
1>          with
1>          [
1>              _OutIt=Pickup *,
1>              _InIt=Pickup *
1>          ]
1>          c:program files (x86)microsoft visual studio 10.0vcincludevector(1165) : while compiling class template member function 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Myvec>)'
1>          with
1>          [
1>              _Myvec=std::_Vector_val<Pickup,std::allocator<Pickup>>,
1>              _Ty=Pickup
1>          ]
1>          c:usersasherdocumentsmy dropboxdirect3dd3d10demo_1.0d3d10demobrain.h(44) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=Pickup
1>          ]
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

您似乎正在调用标准库的函数模板,其类型Agent不能被移动赋值,但调用的算法需要这样做。

正如Als在您的问题的评论中所说,您需要显示调用该算法的代码。

当然您需要提供代理类。

假设c++ 11,你需要实现

struct Agent
{
    // .... other stuff
    Agent(Agent&& other) { /* ... */ }
    Agent& operator=(Agent&& other) { /* ... */ return *this; }
};

,称为移动赋值操作符。这时,您可能想要在move构造函数和move构造函数齐头并进时实现它们:

    Agent(Agent&& other) { /* ... */ }

推荐阅读:

  • http://en.wikipedia.org/wiki/C%2B%2B11 Rvalue_references_and_move_constructors
  • http://akrzemi1.wordpress.com/2011/08/11/move-constructor/

和其他