简体   繁体   中英

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

When I am calling SetParams function from the below function, it is throwing an error "cannot convert argument 1 from 'std::wstring' to 'wchar_t *'"

Can anyone please help me on this?

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;
};

There are two parts of the right answer: 1. You need to use pReader->SetParams(oemPathKey. c_str() , L"read"); 2. Your approach is not safe, you trying to keep pointer to string in the class members. But if the original string will go out of scope then you will receive Access Vioalation, if you are lucky :). So in SetParams you need to copy source string to the class members uisng for example wscpy (I recommend something like wscpy_s), also you need correctly handle allocation/deallocation for string copy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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