# "const wchar_t" is incompatible with parameter of type "LPCSTR"

This topic is 1782 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey there,

still fairly new to C++, having this issue in my code, as you can see from the screenshot

http://img203.imageshack.us/img203/8624/errorae.png

I know it might be something to do with MCBS and UNICODE strings, and that thing were you put an L before a quoted piece. But I'm unsure how to fix it.

fileNamePath is defined like this:

std::wstring fileNamePath;


If anyone could see what Im doing wrong Id be grateful! Thanks

##### Share on other sites

Oh the excitement of waiting for your massive png to load. Use code tags.

But yes, you need to compile your program with UNICODE defined (use unicode char set) in order to use wstring with DX. wstring::c_str returns a const wchar_t* which is LPCWSTR in windows speak, a LPCSTR is a const char* (so result of calling c_str on a std::string).

##### Share on other sites

Still unsure on how to fix this problem..

##### Share on other sites

Select Properties in the Project menu. Then, under Configuration Properties and General, change the parameter Character Set to Use Unicode Character Set.

##### Share on other sites

Select Properties in the Project menu. Then, under Configuration Properties and General, change the parameter Character Set to Use Unicode Character Set.

Thats fixed my related error, but has caused over 40 new errors.. Is there another way?

##### Share on other sites

You have two (sensical) solutions:

- Select "Unicode", then replace every std::string, LPCSTR, LPSTR etc.. in your project with the unicode-counterpart (std::wstring, LPCWSTR, LPWSTR). Also, you need to write an "L" before every string (e.g.:  L"C:/Pictures/Test.png"). This should fix your errors, if not, some of your other included libaries might be using multibyte, I'd get to that if his prooves to be the case.

- Keep your project at Multibyte setting, and replace every std::wstring, LPCWSTR, LPWSTR in your project with the multibyte-counterpart (I've already named those).

Edited by The King2

##### Share on other sites

You have two (sensical) solutions:

- Select "Unicode", then replace every std::string, LPCSTR, LPSTR etc.. in your project with the unicode-counterpart (std::wstring, LPCWSTR, LPWSTR). Also, you need to write an "L" before every string (e.g.:  L"C:/Pictures/Test.png"). This should fix your errors, if not, some of your other included libaries might be using multibyte, I'd get to that if his prooves to be the case.

- Keep your project at Multibyte setting, and replace every std::wstring, LPCWSTR, LPWSTR in your project with the multibyte-counterpart (I've already named those).

Yes this has fixed my related error, but now I have problems with certain operands such as this one:

Other operands are now erroring as well, such as:

if(fileNamePath == texFileNameArray[i])

texFileNameArray.push_back(fileNamePath.c_str());

(the full stop before push_back)

std::string message = "Could not open: ";
message += filename; 

So how do I get round this?

Thanks

##### Share on other sites

Sounds like your program simply isn't consistent with what character set it uses. Proper text handling of almost any kind is not trivial as soon as different encodings comes into play. It seems like your code consists of a mix of narrow and wide characters/strings, and mixing text encodings can be a real problem.

Decide on one single encoding for your entire program, and stick with it everywhere. If you need to handle text in different encodings, then make helper functions that converts text between the proper encoding and your program's encoding. But ultimately, your core program should not handle more than one encoding.

##### Share on other sites

Well this is all new and annoying, but thanks for the help either way... I think I'm going to set it to Unicode and change everything text based and slap an L before all quotes..

Thanks again

##### Share on other sites

There's a third option. If you only need to use Unicode (or ascii) versions of a handful of functions then you can call them directly by appending W (or A) to the function name.

For example the function GetGlyphOutline() is actually two functions. GetGlyphOutlineA() and GetGlyphOutlineW() and a define is set up depending on the Unicode setting to point to the right one. MSDN tells you that at the bottom of the documentation for the function. Only functions which work with text come in two versions like that.

That can be handy if you have lots of existing code that uses char * for things like file names, but want to add Unicode support for the user interface strings. Calling the right function explicitly lets you mix and match and avoid a lot of needless changes.

##### Share on other sites

Win32 is all in C. The typedefs LPCSTR and LPCWSTR are C-style strings. You can't really pass a std::string directly to functions that take this type. Pass it the result of std::string::c_str().

Also, Windows has the define TEXT that will automatically mark a string as with either L or not based on the defines of MBS or UNICODE.