簡體   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