gpt4 book ai didi

c++ - 不匹配 ‘operator=’

转载 作者:行者123 更新时间:2023-11-30 03:13:58 25 4
gpt4 key购买 nike

在下面的示例中,如果我尝试通过引用从 fun() 返回,那么它会按应有的方式给我警告。

然而,当我尝试按值返回时,我希望触发“operator=()”,但它会抛出以下错误,我无法弄清楚原因:

error: no match for ‘operator=’ (operand types are ‘Auto_ptr2<Resource>’ and ‘Auto_ptr2<Resource>’)

note: no known conversion for argument 1 from ‘Auto_ptr2<Resource>’ to
‘Auto_ptr2<Resource>&’

令人惊讶的是,res2=res1 调用了 'operator=()' 就好了!!!

template<class T>
class Auto_ptr2
{
T* m_ptr;
public:
Auto_ptr2(T* ptr=nullptr)
:m_ptr(ptr)
{std::cout<<"\nAuto_ptr CTOR called ";}
~Auto_ptr2()
{
std::cout<<"\n~Auto_ptr2 called\n";
delete m_ptr;
}
// A copy constructor that implements move semantics
Auto_ptr2(Auto_ptr2&
{
std::cout<<"\nAuto_PTR2 copy constructor called";
m_ptr = a.m_ptr;
a.m_ptr = nullptr;
}
Auto_ptr2& operator=(Auto_ptr2& a)
{
std::cout<<"\nAuto_ptr2 operator = called";
if (&a == this)
return *this;
delete m_ptr;
m_ptr = a.m_ptr;
a.m_ptr = nullptr;
return *this;
}

T& operator*() const { return *m_ptr; }
T* operator->() const { return m_ptr; }
bool isNull() const { return m_ptr == nullptr; }
};
class Resource
{
public:
Resource() { std::cout << "\nResource acquired"; }
~Resource() { std::cout << "\nResource destroyed"; }
};

Auto_ptr2<Resource> fun(Auto_ptr2<Resource> res3)
{
std::cout << "\nres1 is " << (res3.isNull() ? "null" : "not null");
return res3;
}

int main()
{
Auto_ptr2<Resource> res1(new Resource);
Auto_ptr2<Resource> res2;

res2 = res1; // res2 assumes ownership, res1 is set to null

res2=fun(res2); //ERROR : error: no match for ‘operator=’ (operand types are ‘Auto_ptr2<Resource>’ and ‘Auto_ptr2<Resource>’)


return 0;
}

最佳答案

如果我没理解错的话,你写的东西类似于 std::unique_ptr

因此,对于 std::unique_ptroperator=() 应该使用 move 语义,因此接收到一个 r-vale 引用,Auto_ptr && ,不是左值引用

     Auto_ptr2& operator=(Auto_ptr2 && a) 
{
std::cout<<"\nAuto_ptr2 operator = called";
if (&a == this)
return *this;
delete m_ptr;
m_ptr = a.m_ptr;
a.m_ptr = nullptr;
return *this;
}

并且使用应该通过std::move()

res2=fun(std::move(res2));

与构造函数相同的问题:避免复制构造函数(可能删除它)并编写一个 move 构造函数。

    Auto_ptr2 (Auto_ptr2 const &) = delete;

Auto_ptr2 (Auto_ptr2 && a)
{
std::cout<<"\nAuto_PTR2 move constructor called";
m_ptr = a.m_ptr;
a.m_ptr = nullptr;
}

关于c++ - 不匹配 ‘operator=’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58290885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com