Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Error C2264

Error C2264

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alunw
    wrote on last edited by
    #1

    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:

    W Z 2 Replies Last reply
    0
    • A alunw

      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:

      W Offline
      W Offline
      Waldermort
      wrote on last edited by
      #2

      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!

      A 1 Reply Last reply
      0
      • W Waldermort

        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!

        A Offline
        A Offline
        alunw
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A alunw

          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:

          Z Offline
          Z Offline
          Zac Howland
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups