繁体   English   中英

错误C2664'无效IVerify :: SetParams(void)':无法将参数1从'std :: wstring'转换为'wchar_t *'

[英]Error C2664 'void IVerify::SetParams(void)': cannot convert argument 1 from 'std::wstring' to 'wchar_t *'

当我从下面的函数调用SetParams函数时,它抛出错误“无法将参数1从'std :: wstring'转换为'wchar_t *'”

有人可以帮我吗?

int main()
{

IVerify* pReader = new BCReader();
std::wstring oemPathKey;

pReader->SetParams(oemPathKey, L"read");

delete pReader;
return 0;
}


void BCReader::SetParams(wchar_t* wszParams, wchar_t* wszParamType)
{
    m_wszParamType = wszParamType;
    m_wszParams = wszParams;
}

The member variables are declared like as shown below:

class IVerify
{
private:

wchar_t* m_wszParams;
wchar_t* m_wszParamType;
};

正确答案有两个部分:1.您需要使用pReader-> SetParams(oemPathKey。c_str () ,L“ read”); 2.您的方法并不安全,您试图在类成员中保持指向字符串的指针。 但是,如果幸运的话,如果原始字符串超出范围,那么您将收到访问违规:)。 因此,在SetParams中,您需要将源字符串复制到uisng的类成员中,例如wscpy(我建议使用wscpy_s之类的东西),也需要正确处理字符串复制的分配/取消分配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM