小编tan*_*nic的帖子

移动构造函数与移动分配

作为问题的扩展,我正在尝试正确设置我的移动分配。

我有以下代码:

// copy assignment operator
LinkedList<T>& operator= (LinkedList<T> other) noexcept
{
    swap(*this, other);
    return *this;
}

// move assignment operator
LinkedList<T>& operator= (LinkedList<T>&& other) noexcept
{
    swap(*this, other);
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用它时,我的代码无法编译。

首先一些代码:

LinkedList<int> generateLinkedList()
{
    LinkedList<int> List;   
    List.add(123);
    return List;
}


int main()
{
    LinkedList<int> L;   
    L = generateLinkedList();
      ^ get an error here...
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

main.cpp(24):错误C2593:'operator ='不明确

linkedlist.h(79):注意:可以是'LinkedList&LinkedList :: operator =(LinkedList &&)noexcept'(指向移动分配运算符)

linkedlist.h(63):注意:或'LinkedList&LinkedList :: operator =(LinkedList)noexcept'(指向副本分配运算符)

main.cpp(24):注意:在尝试匹配参数列表'(LinkedList,LinkedList)'时

我的移动分配运算符是错误的,还是使用错误的方式?

c++ move-assignment-operator

1
推荐指数
1
解决办法
54
查看次数

标签 统计

c++ ×1

move-assignment-operator ×1