Convert const wchar_t* into a WCHAR*

President Camacho

How do I make this to work? C++ types are really confusing:

std::wstring wquery = std::wstring(query.begin(), query.end());
//split names
std::vector<WCHAR*> split_names;
std::stringstream ss;
ss.str(names);
std::string name;
while (std::getline(ss, name, ',')) {
    split_names.push_back(
        (
            std::wstring(
                name.begin(),
                name.end()
            )
        ).c_str()
    ); //error can't assign const wchar_t* into WCHAR*
}
Nikita

C++ tries to keep you from mistakes. Here:

std::wstring(name.begin(), name.end())).c_str()

you create a temporary object std::wstring and get the pointer to the string content. Object will be destroyed right after you leave this block. As a result you will get an invalid pointer.

Don't store pointer to the temporary object into your std::vector<WCHAR*> split_names;.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot convert parameter 1 from 'const wchar_t *' to 'LPCTSTR' in MFC / C++ project

Convert const char* to const wchar_t*

cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}'

Poco::Path compiles with const wchar_t* but behaves unexpectedly

How to make concatenations at const wchar_t* parameters?

Convert Swift String to wchar_t

How to convert int variable and append to const wchar_t*

Unordered_map find const wchar_t*

Error: argument of type "const wchar_t *" is incompatible with parameter of type "WCHAR *"

Cannot convert 'wchar_t' to 'LPCSTR'

C++ Argument of type "const wchar_t" * incompatible with parameter of type "wchar_t"

How to convert char to wchar_t*?

How to convert std::string to wchar_t*

How to transform const wchar_t type to LPTSTR (C++)

String conversion from System::String ^ to const wchar_t *

cannot convert argument 1 from 'ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>' to 'const char *'

Convert unicode string into wchar_t

Convert LOWORD(wParam) to const wchar_t*

Casting std::wstring to const wchar_t x[]

Make a condition on const wchar_t*

win32 atoi issue which can't convert argument 1 from 'wchar_t [5]' to 'const char *

GETTING THE ERROR IN VISUAL STUDIO: error C2664: '_chmod' : cannot convert parameter 1 from 'wchar_t [260]' to 'const char *'

String parameter const char* and const wchar_t*

Convert const wchar_t* to LPWSTR

Argument of type "const wchar_t*" is incompatible with parameter of type "LPTSTR"

Is " const wchar_t* " a pointer?

How to use regex_iterator with const char/wchar_t*?

"cannot convert 'wchar_t*' to 'LPCSTR' {aka 'const char*'}gcc" when trying to use WriteConsoleOutputCharacter()

How convert LPCWSTR to wchar_t*?