Error C2664 Conversion Loses Qualifiers
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 Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask 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 Cannot convert parameter - Conversion loses qualifiers up vote 3 down vote favorite 1 I'm frequently troubled by const correctness, and this seems to be no exception. Please advise me why the following code wont compile: class string_token_stream { public: typedef wchar_t* string_type; string_token_stream(const string_type input_string) : _input_string(input_string) { } private: const string_type _input_string; }; int main(int argc, char **argv) { const wchar_t *str = get_a_string_somewhere(); string_token_stream sts(str); // ^------ Compile-time error C2664. return 0; } The error given is: error C2664: 'string_token_stream::string_token_stream(const string_token_stream::string_type)' : cannot convert parameter 1 from 'const wchar_t *' to 'const string_token_stream::string_type' 1> Conversion loses qualifiers I'm compiling on Visual C++ 2010 Express. Additional compile/linker options available upon request. c++ visual-c++ share|improve this question asked Mar 5 '12 at 4:32 Kivin 3,28552749 add a comment| 2 Answers 2 active oldest votes up vote 5 down vote accepted When you write const string_type input_string the const is ignored for typedef. So you are trying to convert const wchar_t* to a wchar_t*. If you changetypedef wchar_t* string_type; to typedef const wchar_t* string_type; it should compile. const string_type input_string would look like wchar_t *const input_string whereas you expected const wchar_t*. You can see this by changing const wchar_t *str to wchar_t *const str and it will also work. share|improve this answer edited Mar 5 '12 at 5:21 answered Mar 5 '12
Windows Desktop Development > C++ Standards, Extensions, and Interop Question 0 Sign in to vote Ok, if I compile this:void f(const char** c){}int main(){ char** p; f(p);}I get:error C2664: 'f' : cannot convert parameter 1 from 'char **' to 'const char **'1> Conversion loses qualifiersNow, this should not be an error AFAIK, it actualy gains qualifiers because c is const char**, so it's more restrictive, and this should be ok. So why does compiler issue an error? Am I missing something? It works with one indirection (char* to const char*). Sunday, January 11, 2009 12:00 AM Reply | Quote Answers 0 Sign in to vote You may find a rationalization of this behavior in 4.4 ofthe C++ Standard. It says, in part: 4 A conversion can add http://stackoverflow.com/questions/9561666/cannot-convert-parameter-conversion-loses-qualifiers cv-qualifiers at levels other than the first in multi-level pointers, subject to the following rules: (fn.53) [53) These rules ensure that const-safety is preserved by the conversion.] Two pointer types T1 and T2 are similar if there exists a type T and integer n > 0 such that: T1 is cv1,0 pointer to cv1,1 pointer to · · · cv1,n-1 pointer to cv1,n T and T2 is cv2,0 pointer to cv2,1 pointer to · · · cv2,n-1 pointer to cv2,n T where each cvi,j https://social.msdn.microsoft.com/Forums/vstudio/en-US/463022c3-d617-4f9a-bb9d-df4a51a438e9/conversion-loses-qualifiers-error?forum=vclanguage is const, volatile, const volatile, or nothing. The n-tuple of cv-qualifiers after the first in a pointer type, e.g., cv1,1, cv1,2, · · · , cv1,n in the pointer type T1, is called the cv-qualification signature of the pointer type. An expression of type T1 can be converted to type T2 if and only if the following conditions are satisfied: - the pointer types are similar. - for every j > 0, if const is in cv1,j then const is in cv2,j , and similarly for volatile. - if the cv1,j and cv2,j are different, then const is in every cv2,k for 0 < k < j. [ Note: if a program could assign a pointer of type T** to a pointer of type const T** (that is, if line //1 below were allowed), a program could inadvertently modify a const object (as it is done on line //2). For example, int main() {const char c = 'c';char* pc;const char** pcc = &pc; // 1: not allowed*pcc = &c;*pc = 'C'; // 2: modifies a const object}—end note ] - Wayne Marked as answer by dr_shader Sunday, January 11, 2009 4:20 PM Sunday, January 11, 2009 4:52 AM Reply | Quote All replies 0 Sign in to vote While it doesn't answer your question,if you try your code at http://www.comeaucomputing.com/tryitout/ you will see warning: argument of type "char **" is incompatible with parameter of type "const char **" in relaxed mode, and error: argument of type "char **" is i
Module Coding Thread Tools Display Modes Author Message Rirre Veteran Member Join Date: Nov 2006 10-01-2013 , 12:20 Metamod-am compile https://forums.alliedmods.net/showthread.php?t=227433 errors #1 Recently updated to the latest version of metamod-am SDK. But when I compile the project, I get this: Windows: Code: Compiling... dllapi.cpp ..\..\metamod\sdk_util.h(73) : error C2664: 'char *(char *,char *)' : cannot convert parameter 2 from 'const char *' to 'char *' Conversion loses qualifiers ..\..\metamod\sdk_util.h(79) : error C2664: 'void (int,char *,char *,char *)' : cannot convert parameter error c2664 4 from 'const char *' to 'char *' Conversion loses qualifiers ..\..\metamod\sdk_util.h(79) : error C2664: 'void (int,char *,char *,char *)' : cannot convert parameter 3 from 'const char *' to 'char *' Conversion loses qualifiers ..\..\metamod\sdk_util.h(92) : error C2664: 'void (char *,char *,char *)' : cannot convert parameter 3 from 'const char *' to 'char *' Conversion loses qualifiers ..\..\metamod\sdk_util.h(92) : error c2664 conversion error C2664: 'void (char *,char *,char *)' : cannot convert parameter 2 from 'const char *' to 'char *' Conversion loses qualifiers ..\..\metamod\sdk_util.h(105) : error C2664: 'void (char *,char *,char *)' : cannot convert parameter 3 from 'const char *' to 'char *' Conversion loses qualifiers ..\..\metamod\sdk_util.h(105) : error C2664: 'void (char *,char *,char *)' : cannot convert parameter 2 from 'const char *' to 'char *' Conversion loses qualifiers ...and continues through a few files with the exact same errors as above. Generating Code... 70 error(s), 0 warning(s) Linux: [IMG]http://img823.**************/img823/9797/hl22.jpg[/IMG] Rirre View Public Profile Send a private message to Rirre Find More Posts by Rirre DS SourceMod Developer Join Date: Sep 2004 Location: WI, USA 10-01-2013 , 13:44 Re: Metamod-am compile errors #2 My guess is that you're building with the regular Half-Life SDK. We currently build Metamod-AM and AMXX against a modified version of it located here: http://hg.alliedmods.net/hl2sdks/hlsdk/ There were a lot of places where char * was changed to const char * to fix up a number of warnings and errors generated by newer compilers (like cl