[英]binary '=': no operator found which takes a right-hand operand of type 'std::unique_ptr<char [],std::default_delete<_Ty>>'
I have this check and when I'm trying to get the username, it says there is no acceptable conversion from Unique_ptr to CString. 我进行了此检查,当我尝试获取用户名时,它说从Unique_ptr到CString没有可接受的转换。
Here is my code: 这是我的代码:
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();
}
}
Where GetUsername is: 其中GetUsername是:
std::unique_ptr<char[]> GetUsername() { return CStringUtils::Decrypt(username); }
CString user_name = NULL;
I need user_name as CString because at some point I need to append it to a CString message. 我需要user_name作为CString,因为有时需要将它附加到CString消息中。
What's wrong with the way I have it setup above? 我在上面进行设置的方式有什么问题?
Here is the error I get: 这是我得到的错误:
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)
EDIT: 编辑:
I tried this solution as told in the comments: 我按照评论中的说明尝试了此解决方案:
user_name = *(cr.GetUsername());
but I get the error "illegal indirection" 但出现错误“非法间接访问”
The root of your problem is probably the CString type; 问题的根源可能是CString类型。 the error message you get is saying that the implementer(s) of CString didn't define an
operator=
that assigns a CString object from a unique_ptr<char[]>
. 您收到的错误消息是说CString的实现
operator=
未定义用于从unique_ptr<char[]>
分配CString对象的operator=
。
Based on some quick Googling, I'm guessing your CString type is this one because it's from a Microsoft library and you say you're using WinForms. 基于一些快速的Google搜索,我猜您的CString类型就是这种类型,因为它来自Microsoft库,并且您说您正在使用WinForms。 According to the documentation, you can only assign a CString from a raw pointer to a
char
array, that is, const unsigned char*
. 根据文档,您只能将CString从原始指针分配给
char
数组,即const unsigned char*
。 So to assign the result of GetUsername()
to your CString user_name
, you need to get the underlying raw pointer from the unique_ptr
: 因此,
GetUsername()
的结果分配给您的CString user_name
,您需要从unique_ptr
获取基础原始指针:
user_name = cr.GetUsername().get()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.