c++ - 二进制 '=' : no operator found which takes a right-hand operand of type 'std::unique_ptr<char [],std::default_delete<_Ty>>'

标签 c++ winforms c++11

我有这个检查,当我试图获取用户名时,它说没有可接受的从 Unique_ptr 到 CString 的转换。

这是我的代码:

if (tsvn_creds.size() == 1)
    {
        std::map<CStringA,Creds>::iterator it = tsvn_creds.begin();
        if (tsvn_creds.find(it->first) != tsvn_creds.end())
        {
            Creds cr = tsvn_creds[it->first];
            user_name = cr.GetUsername();
        }
    }

GetUsername 在哪里:

std::unique_ptr<char[]> GetUsername() { return CStringUtils::Decrypt(username); }

CString user_name = NULL;

我需要 user_name 作为 CString,因为在某些时候我需要将它附加到 CString 消息中。

我上面的设置方式有什么问题?

这是我得到的错误:

Error   C2679   binary '=': no operator found which takes a right-hand operand of type 'std::unique_ptr<char [],std::default_delete<_Ty>>' (or there is no acceptable conversion)

编辑:

我按照评论中的说明尝试了这个解决方案:

user_name = *(cr.GetUsername());

但我收到错误“非法间接”

最佳答案

问题的根源可能是 CString 类型;您收到的错误消息是说 CString 的实现者没有定义 operator=unique_ptr<char[]> 分配一个 CString 对象.

根据一些快速的谷歌搜索,我猜你的 CString 类型是 this one因为它来自 Microsoft 库,而您说您使用的是 WinForms。根据文档,您只能将原始指针中的 CString 分配给 char。数组,即const unsigned char* .所以要分配 GetUsername() 的结果到你的 CString user_name ,您需要从 unique_ptr 获取底层原始指针:

user_name = cr.GetUsername().get()

关于c++ - 二进制 '=' : no operator found which takes a right-hand operand of type 'std::unique_ptr<char [],std::default_delete<_Ty>>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34907945/

相关文章:

c++ - 如何正确移植 Wine-only 函数 GetMulti ByteString?

c++ - 反汇编器 GLOBAL 关键字

winforms - 触发属性更改时,具有数据绑定(bind)的表单元素不刷新

vb.net - 如何防止 ListView 中的重复项

c++ - 隐式转换和运算符重载

c++ - 评估 cout 是否具有 constexpr 值?

C++编译错误;流运算符重载

c++ - 创建进程间共享的全局命名计数器

WPF 与 WinForms 或丰富的 UI 与稳定的应用程序?您如何看待 Windows Forms 平台的 future ?

c++ - 在定义中使用 typedef 类型