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. problem with dynamic constructors, (cannot convert string literal to (char *) ).

problem with dynamic constructors, (cannot convert string literal to (char *) ).

Scheduled Pinned Locked Moved C / C++ / MFC
help
11 Posts 5 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.
  • T Tarun Jha

    #include
    #include

    using namespace std;

    class String{
    char *name;
    int length;

    public:
    String(){ //default constructor
    length = 0;
    name = new char[length + 1];
    }
    String(char *s) //parameterised constructor
    {
    length = strlen(s);
    name = new char[length + 1] ;

        strcpy(name, s);
    }
    
    void display(void){
        cout<
    

    i should get output :
    joseph
    louis
    lagrange
    Joseph Louis
    Joseph louis lagrange

    but instead i am getting:
    Joseph
    louis
    lagrange
    louis
    lagrange

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2
    name = new char\[length + 1\];            //dyanamic allocation
    
    strcpy(name, a.name);
    strcpy(name, b.name);
    

    You are copying both names to the same place instead of concatenating them using strcat. A better way would be something like:

    name = new char\[length + 2\];            //dyanamic allocation
    strcpy(name, a.name);     // copy the first name
    strcat(name, " ");        // append a space character after the first name
    strcat(name, b.name);     // append the second name
    
    T 1 Reply Last reply
    0
    • T Tarun Jha

      #include
      #include

      using namespace std;

      class String{
      char *name;
      int length;

      public:
      String(){ //default constructor
      length = 0;
      name = new char[length + 1];
      }
      String(char *s) //parameterised constructor
      {
      length = strlen(s);
      name = new char[length + 1] ;

          strcpy(name, s);
      }
      
      void display(void){
          cout<
      

      i should get output :
      joseph
      louis
      lagrange
      Joseph Louis
      Joseph louis lagrange

      but instead i am getting:
      Joseph
      louis
      lagrange
      louis
      lagrange

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #3

      Your join function is not joining but effectively setting only the second string:

      void String :: join(String &a, String &b){
      length = a.length + b.length ;
      delete name;
      name = new char[length + 1]; //dyanamic allocation

      strcpy(name, a.name);
      // This will overwrite name with b.name
      //strcpy(name, b.name);
      // You have to append b.name instead using strcat()
      strcat(name, b.name);
      //  or by copying to the current end
      //strcpy(name + a.length, b.name);
      

      };

      cannot convert string literal to (char *)

      To avoid that message change the constructor to accept a const char* argument:

      String(const char *s)
      {
      length = strlen(s);
      name = new char[length + 1] ;
      strcpy(name, s);
      }

      T 2 Replies Last reply
      0
      • T Tarun Jha

        #include
        #include

        using namespace std;

        class String{
        char *name;
        int length;

        public:
        String(){ //default constructor
        length = 0;
        name = new char[length + 1];
        }
        String(char *s) //parameterised constructor
        {
        length = strlen(s);
        name = new char[length + 1] ;

            strcpy(name, s);
        }
        
        void display(void){
            cout<
        

        i should get output :
        joseph
        louis
        lagrange
        Joseph Louis
        Joseph louis lagrange

        but instead i am getting:
        Joseph
        louis
        lagrange
        louis
        lagrange

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #4

        You know C++ standard library provides the string class, featuring the concatenation operator[^], don't you?

        In testa che avete, signor di Ceprano?

        T 1 Reply Last reply
        0
        • J Jochen Arndt

          Your join function is not joining but effectively setting only the second string:

          void String :: join(String &a, String &b){
          length = a.length + b.length ;
          delete name;
          name = new char[length + 1]; //dyanamic allocation

          strcpy(name, a.name);
          // This will overwrite name with b.name
          //strcpy(name, b.name);
          // You have to append b.name instead using strcat()
          strcat(name, b.name);
          //  or by copying to the current end
          //strcpy(name + a.length, b.name);
          

          };

          cannot convert string literal to (char *)

          To avoid that message change the constructor to accept a const char* argument:

          String(const char *s)
          {
          length = strlen(s);
          name = new char[length + 1] ;
          strcpy(name, s);
          }

          T Offline
          T Offline
          Tarun Jha
          wrote on last edited by
          #5

          Thank you sir.. here's the working code

          #include
          #include

          using namespace std;

          class String{
          char *name;
          int length;

          public:
          String(){ //default constructor
          length = 0;
          name = new char[length + 1];
          }
          String(const char *s) //parameterised constructor
          {
          length = strlen(s);
          name = new char[length + 1] ;

              strcpy(name, s);
          }
          
          void display(void){
              cout<
          
          1 Reply Last reply
          0
          • CPalliniC CPallini

            You know C++ standard library provides the string class, featuring the concatenation operator[^], don't you?

            T Offline
            T Offline
            Tarun Jha
            wrote on last edited by
            #6

            i am just beginning with c++, so did'nt knew. Thank you for informing

            1 Reply Last reply
            0
            • L Lost User
              name = new char\[length + 1\];            //dyanamic allocation
              
              strcpy(name, a.name);
              strcpy(name, b.name);
              

              You are copying both names to the same place instead of concatenating them using strcat. A better way would be something like:

              name = new char\[length + 2\];            //dyanamic allocation
              strcpy(name, a.name);     // copy the first name
              strcat(name, " ");        // append a space character after the first name
              strcat(name, b.name);     // append the second name
              
              T Offline
              T Offline
              Tarun Jha
              wrote on last edited by
              #7

              Thank you

              1 Reply Last reply
              0
              • J Jochen Arndt

                Your join function is not joining but effectively setting only the second string:

                void String :: join(String &a, String &b){
                length = a.length + b.length ;
                delete name;
                name = new char[length + 1]; //dyanamic allocation

                strcpy(name, a.name);
                // This will overwrite name with b.name
                //strcpy(name, b.name);
                // You have to append b.name instead using strcat()
                strcat(name, b.name);
                //  or by copying to the current end
                //strcpy(name + a.length, b.name);
                

                };

                cannot convert string literal to (char *)

                To avoid that message change the constructor to accept a const char* argument:

                String(const char *s)
                {
                length = strlen(s);
                name = new char[length + 1] ;
                strcpy(name, s);
                }

                T Offline
                T Offline
                Tarun Jha
                wrote on last edited by
                #8

                Quote:

                To avoid that message change the constructor to accept a const char* argument:

                It worked as you told, but how and why it was giving error when i was simply using char, and how i will know in which conditions to use const char ? please can you elaborate

                J 1 Reply Last reply
                0
                • T Tarun Jha

                  Quote:

                  To avoid that message change the constructor to accept a const char* argument:

                  It worked as you told, but how and why it was giving error when i was simply using char, and how i will know in which conditions to use const char ? please can you elaborate

                  J Offline
                  J Offline
                  Jochen Arndt
                  wrote on last edited by
                  #9

                  Always use const for pointer and reference parameters when the content is not modified by a function. You have to use it when passing a string literal because that is by definition const (a "string literal" is a const char* which can't be assigned to a non-const char*).

                  T 1 Reply Last reply
                  0
                  • T Tarun Jha

                    #include
                    #include

                    using namespace std;

                    class String{
                    char *name;
                    int length;

                    public:
                    String(){ //default constructor
                    length = 0;
                    name = new char[length + 1];
                    }
                    String(char *s) //parameterised constructor
                    {
                    length = strlen(s);
                    name = new char[length + 1] ;

                        strcpy(name, s);
                    }
                    
                    void display(void){
                        cout<
                    

                    i should get output :
                    joseph
                    louis
                    lagrange
                    Joseph Louis
                    Joseph louis lagrange

                    but instead i am getting:
                    Joseph
                    louis
                    lagrange
                    louis
                    lagrange

                    J Offline
                    J Offline
                    Joe Woodbury
                    wrote on last edited by
                    #10

                    Tarun Jha wrote:

                    length = a.length + b.length ;

                    [Trivial] note that this actually should be:

                    length = a.length + b.length - 1;

                    (And please add a destructor. As a pure FYI, since I assume this is for learning hence not using std::string, you could use std::unique_ptr for name, avoiding the need for anything but a default destructor.)

                    1 Reply Last reply
                    0
                    • J Jochen Arndt

                      Always use const for pointer and reference parameters when the content is not modified by a function. You have to use it when passing a string literal because that is by definition const (a "string literal" is a const char* which can't be assigned to a non-const char*).

                      T Offline
                      T Offline
                      Tarun Jha
                      wrote on last edited by
                      #11

                      thank you :)

                      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