Trouble with constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • San

    Trouble with constructor

    Hi,

    I wrote two simple constructors (one of them default) to initialize a
    student object.
    Student::Studen t(string name)
    {
    student_name = name;
    student_id = LATEST_ID + 1;
    student_address ("");
    student_phone(0 );
    student_email(" ");

    }
    Student::Studen t()
    {
    student_name("" );
    student_id = LATEST_ID + 1;
    student_address ("");
    student_phone(0 );
    student_email(" ");
    }

    When I try to use the following:
    Student stud_1("San");
    The g++ compiler gives an error message "undefined message to
    Student::Studen t(basic_string( char, string_char_tra its(char),
    _default_alloc_ template(0,0))) "

    The compiler has no problem if I use Student stud_1();

    Is the trouble due to the fact that I am trying to assign a string and
    that there is no copy constructor defined?

    Any help would be greatly appreciated.

    Thanks,
    San
  • Leor Zolman

    #2
    Re: Trouble with constructor

    On 9 Apr 2004 12:37:58 -0700, abc_99_420@hotm ail.com (San) wrote:
    [color=blue]
    >Hi,
    >
    >I wrote two simple constructors (one of them default) to initialize a
    >student object.
    >Student::Stude nt(string name)
    > {
    > student_name = name;
    > student_id = LATEST_ID + 1;
    > student_address ("");[/color]
    You've confused member initializer syntax with stuff you do elsewhere than
    in the initializer list. You want something like this:

    Student::Studen t(string name) : student_name(na me),
    student_id(LATE ST_ID + 1), student_phone(0 ) {}

    and just leave the others out (assuming their default constructor behaves
    the same as their constructor taking "")

    Note that the only place member objects can be /initialized/ by a
    constructor is in that constructor's initializer list; otherwise, if you
    leave the job until you're within the body of the constructor, you're stuck
    with having to do an /assignment/ (and the objects you're assigning to will
    already have had their default constructors run on them, whether you like
    it or not.)
    -leor

    [color=blue]
    > student_phone(0 );
    > student_email(" ");
    >
    > }
    >Student::Stude nt()
    > {
    > student_name("" );
    > student_id = LATEST_ID + 1;
    > student_address ("");
    > student_phone(0 );
    > student_email(" ");
    > }
    >
    >When I try to use the following:
    >Student stud_1("San");
    >The g++ compiler gives an error message "undefined message to
    >Student::Stude nt(basic_string (char, string_char_tra its(char),
    >_default_alloc _template(0,0)) )"
    >
    >The compiler has no problem if I use Student stud_1();
    >
    >Is the trouble due to the fact that I am trying to assign a string and
    >that there is no copy constructor defined?
    >
    >Any help would be greatly appreciated.
    >
    >Thanks,
    >San[/color]

    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

    Comment

    • Leor Zolman

      #3
      Re: Trouble with constructor

      On 9 Apr 2004 12:37:58 -0700, abc_99_420@hotm ail.com (San) wrote:
      [color=blue]
      >Hi,
      >
      >I wrote two simple constructors (one of them default) to initialize a
      >student object.
      >Student::Stude nt(string name)
      > {
      > student_name = name;
      > student_id = LATEST_ID + 1;
      > student_address ("");[/color]
      You've confused member initializer syntax with stuff you do elsewhere than
      in the initializer list. You want something like this:

      Student::Studen t(string name) : student_name(na me),
      student_id(LATE ST_ID + 1), student_phone(0 ) {}

      and just leave the others out (assuming their default constructor behaves
      the same as their constructor taking "")

      Note that the only place member objects can be /initialized/ by a
      constructor is in that constructor's initializer list; otherwise, if you
      leave the job until you're within the body of the constructor, you're stuck
      with having to do an /assignment/ (and the objects you're assigning to will
      already have had their default constructors run on them, whether you like
      it or not.)
      -leor

      [color=blue]
      > student_phone(0 );
      > student_email(" ");
      >
      > }
      >Student::Stude nt()
      > {
      > student_name("" );
      > student_id = LATEST_ID + 1;
      > student_address ("");
      > student_phone(0 );
      > student_email(" ");
      > }
      >
      >When I try to use the following:
      >Student stud_1("San");
      >The g++ compiler gives an error message "undefined message to
      >Student::Stude nt(basic_string (char, string_char_tra its(char),
      >_default_alloc _template(0,0)) )"
      >
      >The compiler has no problem if I use Student stud_1();
      >
      >Is the trouble due to the fact that I am trying to assign a string and
      >that there is no copy constructor defined?
      >
      >Any help would be greatly appreciated.
      >
      >Thanks,
      >San[/color]

      --
      Leor Zolman --- BD Software --- www.bdsoft.com
      On-Site Training in C/C++, Java, Perl and Unix
      C++ users: Download BD Software's free STL Error Message Decryptor at:
      An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

      Comment

      • John Carson

        #4
        Re: Trouble with constructor

        "San" <abc_99_420@hot mail.com> wrote in message
        news:ba6b3627.0 404091137.6e971 f65@posting.goo gle.com[color=blue]
        > Hi,
        >
        > I wrote two simple constructors (one of them default) to initialize a
        > student object.
        > Student::Studen t(string name)
        > {
        > student_name = name;
        > student_id = LATEST_ID + 1;
        > student_address ("");
        > student_phone(0 );
        > student_email(" ");
        >
        > }
        > Student::Studen t()
        > {
        > student_name("" );
        > student_id = LATEST_ID + 1;
        > student_address ("");
        > student_phone(0 );
        > student_email(" ");
        > }
        >
        > When I try to use the following:
        > Student stud_1("San");
        > The g++ compiler gives an error message "undefined message to
        > Student::Studen t(basic_string( char, string_char_tra its(char),
        > _default_alloc_ template(0,0))) "
        >
        > The compiler has no problem if I use Student stud_1();
        >
        > Is the trouble due to the fact that I am trying to assign a string and
        > that there is no copy constructor defined?
        >
        > Any help would be greatly appreciated.
        >
        > Thanks,
        > San[/color]

        Unless g++ supports all sorts of non-standard nonsense, neither of your
        constructors can compile. You can't make assignments with calls like

        student_name("" );

        or

        student_phone(0 );

        Show us the real code and we may have some chance of identifying the
        problem.

        Incidentally,

        Student stud_1();

        does not define a Student object and invoke the default constructor.
        Rather, it
        declares a function called stud_1 that takes no arguments and returns a
        Student object. To define a Student object, drop the brackets, i.e., use

        Student stud_1;

        --
        John Carson
        1. To reply to email address, remove donald
        2. Don't reply to email address (post here instead)

        Comment

        • John Carson

          #5
          Re: Trouble with constructor

          "San" <abc_99_420@hot mail.com> wrote in message
          news:ba6b3627.0 404091137.6e971 f65@posting.goo gle.com[color=blue]
          > Hi,
          >
          > I wrote two simple constructors (one of them default) to initialize a
          > student object.
          > Student::Studen t(string name)
          > {
          > student_name = name;
          > student_id = LATEST_ID + 1;
          > student_address ("");
          > student_phone(0 );
          > student_email(" ");
          >
          > }
          > Student::Studen t()
          > {
          > student_name("" );
          > student_id = LATEST_ID + 1;
          > student_address ("");
          > student_phone(0 );
          > student_email(" ");
          > }
          >
          > When I try to use the following:
          > Student stud_1("San");
          > The g++ compiler gives an error message "undefined message to
          > Student::Studen t(basic_string( char, string_char_tra its(char),
          > _default_alloc_ template(0,0))) "
          >
          > The compiler has no problem if I use Student stud_1();
          >
          > Is the trouble due to the fact that I am trying to assign a string and
          > that there is no copy constructor defined?
          >
          > Any help would be greatly appreciated.
          >
          > Thanks,
          > San[/color]

          Unless g++ supports all sorts of non-standard nonsense, neither of your
          constructors can compile. You can't make assignments with calls like

          student_name("" );

          or

          student_phone(0 );

          Show us the real code and we may have some chance of identifying the
          problem.

          Incidentally,

          Student stud_1();

          does not define a Student object and invoke the default constructor.
          Rather, it
          declares a function called stud_1 that takes no arguments and returns a
          Student object. To define a Student object, drop the brackets, i.e., use

          Student stud_1;

          --
          John Carson
          1. To reply to email address, remove donald
          2. Don't reply to email address (post here instead)

          Comment

          • Mike Higginbottom

            #6
            Re: Trouble with constructor

            On 9 Apr 2004 12:37:58 -0700, San <abc_99_420@hot mail.com> wrote:
            [color=blue]
            > I wrote two simple constructors (one of them default) to initialize a
            > student object.[/color]

            [snip]
            [color=blue]
            > When I try to use the following:
            > Student stud_1("San");
            > The g++ compiler gives an error message "undefined message to
            > Student::Studen t(basic_string( char, string_char_tra its(char),
            > _default_alloc_ template(0,0))) "
            >[/color]

            It would be nice to see the class declaration but the following works fine
            on .NET:

            class Student
            {
            public:
            Student();
            Student(std::st ring name);
            private:
            std::string student_name;
            };

            Student::Studen t(std::string name)
            {
            student_name = name;
            }

            Student::Studen t()
            {
            student_name = ""; //Note: this is not student_name("" )
            }

            Student stud_1("San");
            [color=blue]
            > The compiler has no problem if I use Student stud_1();
            >[/color]
            I'd expect it to fail on:

            student_name("" );
            [color=blue]
            > Is the trouble due to the fact that I am trying to assign a string and
            > that there is no copy constructor defined?[/color]
            No. The default copy ctor will work fine for this class and will just
            call the copy ctor for std::string which does exist.
            [color=blue]
            >
            > Any help would be greatly appreciated.
            >[/color]
            A better (more efficient) way would be to use initialization rather than
            assignment (see [1] or Scott Meyers' Effective C++)in the two ctors you
            define, as follows:

            Student::Studen t(std::string name) : student_name(na me)
            {
            }

            which may have been what you were trying to do inthe first place.

            [1] http://www.parashift.com/c++-faq-lit....html#faq-10.6

            --
            Mike Higginbottom

            Comment

            • Mike Higginbottom

              #7
              Re: Trouble with constructor

              On 9 Apr 2004 12:37:58 -0700, San <abc_99_420@hot mail.com> wrote:
              [color=blue]
              > I wrote two simple constructors (one of them default) to initialize a
              > student object.[/color]

              [snip]
              [color=blue]
              > When I try to use the following:
              > Student stud_1("San");
              > The g++ compiler gives an error message "undefined message to
              > Student::Studen t(basic_string( char, string_char_tra its(char),
              > _default_alloc_ template(0,0))) "
              >[/color]

              It would be nice to see the class declaration but the following works fine
              on .NET:

              class Student
              {
              public:
              Student();
              Student(std::st ring name);
              private:
              std::string student_name;
              };

              Student::Studen t(std::string name)
              {
              student_name = name;
              }

              Student::Studen t()
              {
              student_name = ""; //Note: this is not student_name("" )
              }

              Student stud_1("San");
              [color=blue]
              > The compiler has no problem if I use Student stud_1();
              >[/color]
              I'd expect it to fail on:

              student_name("" );
              [color=blue]
              > Is the trouble due to the fact that I am trying to assign a string and
              > that there is no copy constructor defined?[/color]
              No. The default copy ctor will work fine for this class and will just
              call the copy ctor for std::string which does exist.
              [color=blue]
              >
              > Any help would be greatly appreciated.
              >[/color]
              A better (more efficient) way would be to use initialization rather than
              assignment (see [1] or Scott Meyers' Effective C++)in the two ctors you
              define, as follows:

              Student::Studen t(std::string name) : student_name(na me)
              {
              }

              which may have been what you were trying to do inthe first place.

              [1] http://www.parashift.com/c++-faq-lit....html#faq-10.6

              --
              Mike Higginbottom

              Comment

              • Leor Zolman

                #8
                Re: Trouble with constructor

                On Sat, 10 Apr 2004 06:17:33 +1000, "John Carson"
                <donaldquixote@ datafast.net.au > wrote:
                [color=blue]
                >
                >Incidentally ,
                >
                >Student stud_1();
                >
                >does not define a Student object and invoke the default constructor.
                >Rather, it
                >declares a function called stud_1 that takes no arguments and returns a
                >Student object. To define a Student object, drop the brackets, i.e., use
                >
                >Student stud_1;[/color]

                Good catch. I missed it (but I must admit I wasn't looking all that hard
                any more by the time I reached that section of the post).

                Ironically, this is the very item I contributed to Bruce Eckel & Chuck
                Allison's "C++ Annoyances" session at SD West recently. The reason it
                annoys me should now be quite apparent: I don't /notice/ it ;-)
                -leor


                --
                Leor Zolman --- BD Software --- www.bdsoft.com
                On-Site Training in C/C++, Java, Perl and Unix
                C++ users: Download BD Software's free STL Error Message Decryptor at:
                An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

                Comment

                • Leor Zolman

                  #9
                  Re: Trouble with constructor

                  On Sat, 10 Apr 2004 06:17:33 +1000, "John Carson"
                  <donaldquixote@ datafast.net.au > wrote:
                  [color=blue]
                  >
                  >Incidentally ,
                  >
                  >Student stud_1();
                  >
                  >does not define a Student object and invoke the default constructor.
                  >Rather, it
                  >declares a function called stud_1 that takes no arguments and returns a
                  >Student object. To define a Student object, drop the brackets, i.e., use
                  >
                  >Student stud_1;[/color]

                  Good catch. I missed it (but I must admit I wasn't looking all that hard
                  any more by the time I reached that section of the post).

                  Ironically, this is the very item I contributed to Bruce Eckel & Chuck
                  Allison's "C++ Annoyances" session at SD West recently. The reason it
                  annoys me should now be quite apparent: I don't /notice/ it ;-)
                  -leor


                  --
                  Leor Zolman --- BD Software --- www.bdsoft.com
                  On-Site Training in C/C++, Java, Perl and Unix
                  C++ users: Download BD Software's free STL Error Message Decryptor at:
                  An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

                  Comment

                  • Mike Higginbottom

                    #10
                    Re: Trouble with constructor

                    On Fri, 09 Apr 2004 20:32:06 GMT, Leor Zolman <leor@bdsoft.co m> wrote:
                    [color=blue][color=green]
                    >> Student stud_1;[/color]
                    >
                    > Good catch. I missed it (but I must admit I wasn't looking all that hard
                    > any more by the time I reached that section of the post).
                    >
                    > Ironically, this is the very item I contributed to Bruce Eckel & Chuck
                    > Allison's "C++ Annoyances" session at SD West recently. The reason it
                    > annoys me should now be quite apparent: I don't /notice/ it ;-)
                    > -leor
                    >
                    >[/color]

                    Not just me then ;)


                    --
                    Mike Higginbottom

                    Comment

                    • Mike Higginbottom

                      #11
                      Re: Trouble with constructor

                      On Fri, 09 Apr 2004 20:32:06 GMT, Leor Zolman <leor@bdsoft.co m> wrote:
                      [color=blue][color=green]
                      >> Student stud_1;[/color]
                      >
                      > Good catch. I missed it (but I must admit I wasn't looking all that hard
                      > any more by the time I reached that section of the post).
                      >
                      > Ironically, this is the very item I contributed to Bruce Eckel & Chuck
                      > Allison's "C++ Annoyances" session at SD West recently. The reason it
                      > annoys me should now be quite apparent: I don't /notice/ it ;-)
                      > -leor
                      >
                      >[/color]

                      Not just me then ;)


                      --
                      Mike Higginbottom

                      Comment

                      • Gary Labowitz

                        #12
                        Re: Trouble with constructor

                        "Mike Higginbottom" <news@peak41.co .uk> wrote in message
                        news:opr57hwusi ct0wm7@fuzzbox. ..[color=blue]
                        > On 9 Apr 2004 12:37:58 -0700, San <abc_99_420@hot mail.com> wrote:
                        >[color=green]
                        > > I wrote two simple constructors (one of them default) to[/color][/color]
                        initialize a[color=blue][color=green]
                        > > student object.[/color]
                        >
                        > [snip][color=green]
                        > > When I try to use the following:
                        > > Student stud_1("San");
                        > > The g++ compiler gives an error message "undefined message to
                        > > Student::Studen t(basic_string( char, string_char_tra its(char),
                        > > _default_alloc_ template(0,0))) "[/color]
                        > It would be nice to see the class declaration but the following[/color]
                        works fine[color=blue]
                        > on .NET:
                        >
                        > class Student
                        > {
                        > public:
                        > Student();
                        > Student(std::st ring name);
                        > private:
                        > std::string student_name;[/color]

                        I'd like to see if OP declared student_name that way. I'm thinking it
                        was a c-style string.
                        [color=blue]
                        > };
                        >[/color]

                        All other problems were taken care of.
                        --
                        Gary


                        Comment

                        • Gary Labowitz

                          #13
                          Re: Trouble with constructor

                          "Mike Higginbottom" <news@peak41.co .uk> wrote in message
                          news:opr57hwusi ct0wm7@fuzzbox. ..[color=blue]
                          > On 9 Apr 2004 12:37:58 -0700, San <abc_99_420@hot mail.com> wrote:
                          >[color=green]
                          > > I wrote two simple constructors (one of them default) to[/color][/color]
                          initialize a[color=blue][color=green]
                          > > student object.[/color]
                          >
                          > [snip][color=green]
                          > > When I try to use the following:
                          > > Student stud_1("San");
                          > > The g++ compiler gives an error message "undefined message to
                          > > Student::Studen t(basic_string( char, string_char_tra its(char),
                          > > _default_alloc_ template(0,0))) "[/color]
                          > It would be nice to see the class declaration but the following[/color]
                          works fine[color=blue]
                          > on .NET:
                          >
                          > class Student
                          > {
                          > public:
                          > Student();
                          > Student(std::st ring name);
                          > private:
                          > std::string student_name;[/color]

                          I'd like to see if OP declared student_name that way. I'm thinking it
                          was a c-style string.
                          [color=blue]
                          > };
                          >[/color]

                          All other problems were taken care of.
                          --
                          Gary


                          Comment

                          • San

                            #14
                            Re: Trouble with constructor(cod e included)

                            Here's the class definition:
                            //-----------------------------------------------------------------------------
                            #ifndef Student_h
                            #define Student_h

                            #include <string>

                            namespace stud{

                            class Student
                            {
                            private:
                            static const long LATEST_ID = 99999999;
                            string student_name;
                            long student_id;
                            string student_address ;
                            long student_phone;
                            string student_email;

                            public:
                            Student();

                            Student(std::st ring name);

                            }; //class student
                            } //namespace stud

                            #endif

                            //-----------------------------------------------------------------------------

                            I changed the implementation or cpp file to include the foll:

                            #include "Student.h"
                            #include <string>

                            namespace stud{

                            Student::Studen t(std::string name)
                            {
                            student_name = name;
                            student_id = LATEST_ID + 1;
                            }

                            Student::Studen t()
                            {
                            student_name = "";
                            student_id = LATEST_ID + 1;
                            }
                            } //namespace stud

                            //-----------------------------------------------------------------------------


                            The main file code contains the foll:

                            #include "Student.h"
                            using namespace std;
                            using namespace stud;

                            int main()
                            {
                            Student stud_1;
                            Student stud_2("San");
                            return 0;
                            }



                            I am aware of the advantages using initialization before the
                            constructor body and did start out with that. But when I came across
                            the errors, I thought maybe I was doing something wrong in the
                            initialization part and so resorted to assigment in the body.

                            If I have to initialize a string variable with an empty string and a
                            long integer with 0 isnt this the right way to do it?
                            Student::Studen t():student_nam e(""),student_p hone(0){}


                            Thanks for all the advice. The Student stud_1(); thing was really
                            silly of me :-)


                            As an aside, I have just realised that posting to Usenet using Google
                            incurs a long delay. Being new to the usenet community, I am trying to
                            find newsreaders that update posts faster. Kindly bear with me.

                            Thanks,
                            Santosh

                            Comment

                            • San

                              #15
                              Re: Trouble with constructor(cod e included)

                              Here's the class definition:
                              //-----------------------------------------------------------------------------
                              #ifndef Student_h
                              #define Student_h

                              #include <string>

                              namespace stud{

                              class Student
                              {
                              private:
                              static const long LATEST_ID = 99999999;
                              string student_name;
                              long student_id;
                              string student_address ;
                              long student_phone;
                              string student_email;

                              public:
                              Student();

                              Student(std::st ring name);

                              }; //class student
                              } //namespace stud

                              #endif

                              //-----------------------------------------------------------------------------

                              I changed the implementation or cpp file to include the foll:

                              #include "Student.h"
                              #include <string>

                              namespace stud{

                              Student::Studen t(std::string name)
                              {
                              student_name = name;
                              student_id = LATEST_ID + 1;
                              }

                              Student::Studen t()
                              {
                              student_name = "";
                              student_id = LATEST_ID + 1;
                              }
                              } //namespace stud

                              //-----------------------------------------------------------------------------


                              The main file code contains the foll:

                              #include "Student.h"
                              using namespace std;
                              using namespace stud;

                              int main()
                              {
                              Student stud_1;
                              Student stud_2("San");
                              return 0;
                              }



                              I am aware of the advantages using initialization before the
                              constructor body and did start out with that. But when I came across
                              the errors, I thought maybe I was doing something wrong in the
                              initialization part and so resorted to assigment in the body.

                              If I have to initialize a string variable with an empty string and a
                              long integer with 0 isnt this the right way to do it?
                              Student::Studen t():student_nam e(""),student_p hone(0){}


                              Thanks for all the advice. The Student stud_1(); thing was really
                              silly of me :-)


                              As an aside, I have just realised that posting to Usenet using Google
                              incurs a long delay. Being new to the usenet community, I am trying to
                              find newsreaders that update posts faster. Kindly bear with me.

                              Thanks,
                              Santosh

                              Comment

                              Working...