我有这个检查,当我试图获取用户名时,它说没有可接受的从 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/