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

Started by
9 comments, last by Alex Melbourne 11 years, 1 month ago

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

Advertisement

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).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Still unsure on how to fix this problem..

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

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?

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).

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:

error1qv.jpg

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

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.

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

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.

This topic is closed to new replies.

Advertisement