Wednesday, March 3, 2021

Error C2664 cannot convert argument 1 from 'const char [30]' to 'char *'

Error C2664 'BOOL CreateGLWindow(char *,int,int,int,bool)': cannot convert argument 1 from 'const char [30]' to 'char *' Template C:\Demo_Nehe\Lesson02\Lesson2.cpp

the project is :C:\Demo_Nehe\Lesson02\Lesson2.cpp

  1. /Zc:wchar_t (wchar_t Is Native Type)
  2. Parse wchar_t as a built-in type according to the C++ standard.

    Remarks If /Zc:wchar_t is on, wchar_t is a keyword for a built-in integral type in code compiled as C++. If /Zc:wchar_t- (with a minus sign) is specified, or in code compiled as C, wchar_t is not a built-in type. Instead, wchar_t is defined as a typedef for unsigned short in the canonical header stddef.h. (The Microsoft implementation defines it in another header that is included by stddef.h and other standard headers.)

  3. Cannot convert argument 1 from 'const wchar_t [16]' to 'LPTSTR'
  4. The solution is to turn off the Conformance mode

  5. Compiler Error C2664
  6. 'function' : cannot convert argument n from 'type1' to 'type2'

    This parameter conversion problem might happen if an instance of a class is created and an implicit conversion is attempted on a constructor marked with the explicit keyword. For more information about explicit conversions, see User-Defined Type Conversions.

    If a temporary object is passed to a function that takes a reference to an object as a parameter, that reference must be a const reference.

    If the function is passed a parameter that is not of the type that the function expects, a temporary object is created by using the appropriate constructor. This temporary object is then passed to the function. In this case, the temporary object is used to initialize the reference. In earlier versions of the language, all references could be initialized by temporary objects.

  7. Cannot convert argument 1 from 'const char [5]' to 'LPCTSTR'
  8. Try using Mulit-byte character encoding in Project Settings instead of Unicode. In case of VS2013, VS2015 you need to download and install the MBCS library for visual studio from this link https://www.microsoft.com/en-us/download/confirmation.aspx?id=40770

  9. error C2664 cannot convert argument 1 from 'const char [11]' to 'LPSTR'
  10. How to fix this error C2664? You have some options:

    1. Disable Conformance Mode (easiest)
    2. Cast to a char
    3. Define as non const
    4. Make function take const arguments

    The reason this error occurs is because on recent ISO standards of C++ which have recently been adopted by Visual Studio the strong typed aspect of C++ has become even stricter especially for strings.

    String literals are expressed such as "string here" and are internally defined as constant. If the function you're passing them to takes a non-const argument, then you will have this issue with conformance mode set to ON. This sets the /permissive compiler flag. This is what causes the problem.

  11. help every time of this error in either of copilar
  12. All these errors are caused by one and the same issue: you are passing arguments of the wrong type to the indicated function, and every time one type is some variant of char* whereas the other type is some multibyte or wide character string.

    It is unclear why you get so many different errors that are all efffectively the same - maybe you tried to recreate a project based only on the source files, without knowing the project settings? If so, check the project properties and change the property 'Character Set' to 'Not set'.

    Otherwise I don't see any other way but fixing every error individually by changing the appropriate variable type or function call to the appropriate char types and function variants (for each string function there are variants for multibyte and single byte strings). The cleanest way would be to always use the type TCHAR instead of char, and use the string function variants that expect the TCHAR types (TCHAR*, LPCTSTR, etc.). There's more info on character sets on MSDN and elsewhere in the web.

No comments:

Post a Comment