Error C2664 Const Char
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us cannot convert parameter 1 from 'const char *' to 'lpcwstr' Learn more about Stack Overflow the company Business Learn more about hiring developers
How To Convert Const Char To Lpctstr In C++
or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack error c2664 cannot convert parameter 1 from 'const char *' to 'char *' Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up C++ cannot convert parameter 1 from 'const
Cannot Convert From Const Char * To Lpcwstr
char *' to 'char *' up vote -4 down vote favorite I'm not a C++ developer at all, but was giving the task of upgrading some old code from Visual Studio 6 to Visual Studio 2010. I'm getting an error from the following code. MessageGroup::MessageGroup(const char *name, WordCollection *words) { _name.assign(_strupr(name)); setWordCollection(words); } Error: error C2664: '_strupr' : cannot convert parameter 1 from 'const char *' to cannot convert argument 1 from const char * to lpctstr 'char *' c++ share|improve this question edited Jan 27 at 20:04 MASh 769324 asked Jan 27 at 19:30 Darren 12 3 And? The error message is kinda self explanatory. –NathanOliver Jan 27 at 19:31 name is of type const char *. _strupr is looking for an input of type char *. –E. Moffat Jan 27 at 19:33 2 Interesting thing about _strupr is it modifies the provided string, converting it to upper case. Trying to modify a constant string is not that good an idea. The compiler is simply trying to tell you to not do something stupid. –user4581301 Jan 27 at 19:34 why not using MessageGroup::MessageGroup(char *name, Wordcollection *words) –MASh Jan 27 at 19:36 Possible duplicate of cannot convert parameter from `const char *` to `char *` –Julien-L Jan 27 at 19:36 | show 4 more comments 3 Answers 3 active oldest votes up vote 2 down vote accepted name is a constant c-style string. It promises the function's caller that the provided string will not be modified inside the MessageGroup constructor or by any functions called by MessageGroup. _strupr(name) is going to convertname` to upper case, violating the no-modifications
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of
Argument Of Type "const Wchar_t *" Is Incompatible With Parameter Of Type "lpcstr"
this site About Us Learn more about Stack Overflow the company Business
Convert Char* To Lpcwstr
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask string to lpctstr Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up http://stackoverflow.com/questions/35046373/c-cannot-convert-parameter-1-from-const-char-to-char cannot convert parameter 1 from 'const char [6]' to 'const wchar_t * up vote 0 down vote favorite I'm new to MFC and I don't know what to do with this error. ERROR error C2664: 'void ATL::CStringT::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [6]' to 'const wchar_t *' heres the line: m_Echo1.Format("%d %",state.dwMemoryLoad); mfc cstring share|improve this question asked http://stackoverflow.com/questions/18155195/cannot-convert-parameter-1-from-const-char-6-to-const-wchar-t Aug 9 '13 at 20:34 user2668338 313 add a comment| 2 Answers 2 active oldest votes up vote 1 down vote accepted By default a Windows app is set to use 16-bit characters, not 8-bit characters. Change your quoted string to L"%d %" to specify a string of 16-bit characters. share|improve this answer answered Aug 9 '13 at 23:42 ScottMcP-MVP 8,9592613 1 Two comments: It's almost always better to use the T macros - so _T("%d %%"). And a single, standalone % is not a valid format specifier. I believe that newer versions of the Microsoft libraries raise a security exception at runtime when they encounter one. –Nik Bougalis Aug 10 '13 at 15:26 add a comment| up vote 1 down vote There are 2 distinct errors with the line of code you posted: The format string contains an illegal format specifier (trailing %). If you want a format string to contain a literal percent-sign it has to be escaped using %%. You are using a string literal that does not match the required encoding, i.e. a mismatch between ANSI and UNICODE character encoding. If m_Echo
Studio Languages , Windows Desktop Development > C++ Standards, Extensions, and Interop General discussion 5 Sign in to vote Note: This is a FAQ, not a question https://social.msdn.microsoft.com/Forums/vstudio/en-US/c1b08c0a-a803-41c3-ac8c-84eba3be1ddb/faq-cannot-convert-from-const-char-to-lpctstr?forum=vclanguage being asked. Question I'm trying to compile a piece of code such as: MessageBox("Hello world!"); ... when I compile the project, the compiler yields: error C2664: 'CWnd::MessageBoxW' : cannot convert parameter 1 from 'const char [12]' to 'LPCTSTR' What am I doing wrong? Problem This error message means that you are trying to pass a multi-byte string (const char [12]) to a function const char which expects a unicode string (LPCTSTR). The LPCTSTR type extends to const TCHAR*, where TCHAR is char when you compile for multi-byte and wchar_t for unicode. Since the compiler doesn't accept the char array, we can safely assume that the actual type of TCHAR, in this compilation, is wchar_t. Resolution You will have to do one of two things: Change your project configuration to use const char * multibyte strings. Press ALT+F7 to open the properties, and navigate to Configuration Properties > General. Switch Character Set to "Use Multi-Byte Character Set". Indicate that the string literal, in this case "Hello world!" is of a specific encoding. This can be done through either prefixing it with L, such as L"Hello world!", or surrounding it with the generic _T("Hello world!") macro. The latter will expand to the L prefix if you are compiling for unicode (see #1), and nothing (indicating multi-byte) otherwise. Variations Another error message, indicating the same problem, would be: cannot convert parameter 1 from 'const char [12]' to 'LPCWSTR' Where LPCWSTR maps to a wchar_t pointer, regardless of your build configuration. This problem can be resolved primarily by using solution #2, but in some cases also #1. A lot of the Microsoft provided libraries, such as the Platform SDK, have got two variations of each function which takes strings as parameters. In case of a unicode build, the actual functions are postfixed W, such as the MessageBoxW seen above. In case of multi-byte, the function would be MessageBoxA (ASCII). Which of these functions is actually used when you compile your application, depen