samedi 24 avril 2021

assigning unsigned char * to std::string

ALL,

Consider following code:

std::string str;
unsigned char *charArray = "ABC";

Now I want to assign the charArray to str.

Calling str.assign() gives an error on MSVC 2017.

There is a constructor for the string that takes the char *, but apparently there is no assignment.

And I need that cross-platform if possible.

Now that charArray comes from the database API, therefore I can't just assign "ABC" directly to str.

Is such assignment exist?

TIA!

Error is

cannot convert argument 1 from 'SQLCHAR *' to 'std::initializer_list<_Elem>' 1> with 1> [ 1> _Elem=wchar_t 1> ] note: No constructor could take the source type, or constructor overload resolution was ambiguous

And SQLCHAR is a typedef to unsigned char under MSVC 2017.

EDIT2

To whim suggested to close because it already has the answer - it doesn't. The types are cmpletely different there. Please reconsider.

Aucun commentaire:

Enregistrer un commentaire