Error C2264
-
Microsoft C++ compilers throw the following error test.cpp test.cpp(14) : error C2664: 'fred' : cannot convert parameter 1 from 'char ** ' to 'const char ** ' Conversion loses qualifiers on the program below.
int fred(const char ** arg)
{
return 0;
}int fred1(const char * arg)
{
return 0;
}int main(int argc,char **argv)
{
fred1(*argv);
return fred(argv);
}I am baffled by this error. As I would expect, the compiler is happy for me to pass a char * to something expecting a const char * (fred1) so what is its problem with passing a char ** to something expecting a const char **? I would understand if it complained when I pass a const char ** to something expecting a char **, but this way round should surely work. There is no problem if fred() expects char const * const *, or even char * const * I'd be interested to know what other compilers think of this code. What qualifier does it think is being lost? :confused:
-
Microsoft C++ compilers throw the following error test.cpp test.cpp(14) : error C2664: 'fred' : cannot convert parameter 1 from 'char ** ' to 'const char ** ' Conversion loses qualifiers on the program below.
int fred(const char ** arg)
{
return 0;
}int fred1(const char * arg)
{
return 0;
}int main(int argc,char **argv)
{
fred1(*argv);
return fred(argv);
}I am baffled by this error. As I would expect, the compiler is happy for me to pass a char * to something expecting a const char * (fred1) so what is its problem with passing a char ** to something expecting a const char **? I would understand if it complained when I pass a const char ** to something expecting a char **, but this way round should surely work. There is no problem if fred() expects char const * const *, or even char * const * I'd be interested to know what other compilers think of this code. What qualifier does it think is being lost? :confused:
This is an annoying error causing me to have to use casts all over my code. I don't know about others, but I think it good practice to define a function paramater with const if the function is not going to modify it. And why 'Error' surely 'Warning' would suffice!
-
This is an annoying error causing me to have to use casts all over my code. I don't know about others, but I think it good practice to define a function paramater with const if the function is not going to modify it. And why 'Error' surely 'Warning' would suffice!
It is correct for this message to be an error, because C++ has much more strict rules for pointer compatibility than C. If my code is compiled as C you get warnings C4090 and C4024 instead of errors. But in this particular case I cannot see what qualifier is being lost. Have you got any other examples where this error is produced for no good reason? I too use const heavily - whenever I pass pointers or references I make them const unless they really need not to be, and I make this * const in methods as well whenever possible. I can't ever recall seeing the compiler objecting to passing a non-const something to something expecting a const something anywhere else.
-
Microsoft C++ compilers throw the following error test.cpp test.cpp(14) : error C2664: 'fred' : cannot convert parameter 1 from 'char ** ' to 'const char ** ' Conversion loses qualifiers on the program below.
int fred(const char ** arg)
{
return 0;
}int fred1(const char * arg)
{
return 0;
}int main(int argc,char **argv)
{
fred1(*argv);
return fred(argv);
}I am baffled by this error. As I would expect, the compiler is happy for me to pass a char * to something expecting a const char * (fred1) so what is its problem with passing a char ** to something expecting a const char **? I would understand if it complained when I pass a const char ** to something expecting a char **, but this way round should surely work. There is no problem if fred() expects char const * const *, or even char * const * I'd be interested to know what other compilers think of this code. What qualifier does it think is being lost? :confused:
I believe what you mean to do is say that the data that argv is pointing to will not be changed. As such, you should write it this way (which won't produce an error):
int f(const char* const* a) { return 0; } int g(const char* a) { return 0; } int main(int argc, char** argv) { g(*argv); return f(argv); }
And by the way, GCC also gives an error with the code you posted.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac