Trouble with constructor

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

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

    On 9 Apr 2004 22:13:13 -0700, abc_99_420@hotm ail.com (San) wrote:
    [color=blue][color=green]
    >>//-----------------------------------------------------------------------------[/color][/color]


    Hi,
    Looking better. Here are some tips:
    [color=blue]
    >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[/color]
    [color=blue]
    >
    >I changed the implementation or cpp file to include the foll:
    >
    >#include "Student.h"
    >#include <string>[/color]

    It's convention to put the standard headers /first/, and you user-defined
    headers last. Not absolutely required, but A Good Idea.
    [color=blue]
    >
    >namespace stud{
    >
    >Student::Stude nt(std::string name)
    >{
    > student_name = name;
    > student_id = LATEST_ID + 1;[/color]

    Since LATEST_ID is a static const, what are you thinking here by assigning
    the /same/ value to each student_id? It would make more sense if LATEST_ID
    were really latest_id, non-const, initialized to whatever, and incremented
    in each constructor (if you haven't dealt with the issue of initializing
    static data yet, note that you must declare such data in-class and still
    define and initialize it outside the class, usually in the implementation
    file.)

    Also, student_phone, being a long, does need to be initialized. If I
    omitted it earlier in my example initialization list, it is simply because
    I didn't notice it there among all the strings, and/or I figured it would
    be a string. In fact, it would be better off as a string. I don't think
    you'd be able to represent my full phone area code/number with a 32-bit
    long ;-)
    [color=blue]
    >}
    >
    >Student::Stude nt()
    >{
    > student_name = "";
    > student_id = LATEST_ID + 1;[/color]

    Same story with LATEST_ID and student_phone.
    [color=blue]
    >}
    >} //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
    >initializati on part and so resorted to assigment in the body.[/color]

    Hope my earlier explanation of that made enough sense.
    [color=blue]
    >
    >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::Stude nt():student_na me(""),student_ phone(0){}[/color]

    In /your/ case, this works, but you'd be better off allowing the string to
    be default-initialized, which you can do by simply omitting the initializer
    altogether.
    [color=blue]
    >
    >
    >Thanks for all the advice. The Student stud_1(); thing was really
    >silly of me :-)[/color]

    No, it wasn't. I, and most other C++ programmers (I dare say) have done it,
    and perhaps still do it, on a regular basis ;-)
    [color=blue]
    >
    >
    >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.[/color]

    Try Agent. There's Free Agent, but the full-blown version is well worth the
    money.
    -leor
    [color=blue]
    >
    >Thanks,
    >Santosh[/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

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

      On 9 Apr 2004 22:13:13 -0700, abc_99_420@hotm ail.com (San) wrote:
      [color=blue][color=green]
      >>//-----------------------------------------------------------------------------[/color][/color]


      Hi,
      Looking better. Here are some tips:
      [color=blue]
      >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[/color]
      [color=blue]
      >
      >I changed the implementation or cpp file to include the foll:
      >
      >#include "Student.h"
      >#include <string>[/color]

      It's convention to put the standard headers /first/, and you user-defined
      headers last. Not absolutely required, but A Good Idea.
      [color=blue]
      >
      >namespace stud{
      >
      >Student::Stude nt(std::string name)
      >{
      > student_name = name;
      > student_id = LATEST_ID + 1;[/color]

      Since LATEST_ID is a static const, what are you thinking here by assigning
      the /same/ value to each student_id? It would make more sense if LATEST_ID
      were really latest_id, non-const, initialized to whatever, and incremented
      in each constructor (if you haven't dealt with the issue of initializing
      static data yet, note that you must declare such data in-class and still
      define and initialize it outside the class, usually in the implementation
      file.)

      Also, student_phone, being a long, does need to be initialized. If I
      omitted it earlier in my example initialization list, it is simply because
      I didn't notice it there among all the strings, and/or I figured it would
      be a string. In fact, it would be better off as a string. I don't think
      you'd be able to represent my full phone area code/number with a 32-bit
      long ;-)
      [color=blue]
      >}
      >
      >Student::Stude nt()
      >{
      > student_name = "";
      > student_id = LATEST_ID + 1;[/color]

      Same story with LATEST_ID and student_phone.
      [color=blue]
      >}
      >} //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
      >initializati on part and so resorted to assigment in the body.[/color]

      Hope my earlier explanation of that made enough sense.
      [color=blue]
      >
      >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::Stude nt():student_na me(""),student_ phone(0){}[/color]

      In /your/ case, this works, but you'd be better off allowing the string to
      be default-initialized, which you can do by simply omitting the initializer
      altogether.
      [color=blue]
      >
      >
      >Thanks for all the advice. The Student stud_1(); thing was really
      >silly of me :-)[/color]

      No, it wasn't. I, and most other C++ programmers (I dare say) have done it,
      and perhaps still do it, on a regular basis ;-)
      [color=blue]
      >
      >
      >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.[/color]

      Try Agent. There's Free Agent, but the full-blown version is well worth the
      money.
      -leor
      [color=blue]
      >
      >Thanks,
      >Santosh[/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 Harrison

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

        > >[color=blue][color=green]
        > >I changed the implementation or cpp file to include the foll:
        > >
        > >#include "Student.h"
        > >#include <string>[/color]
        >
        > It's convention to put the standard headers /first/, and you user-defined
        > headers last. Not absolutely required, but A Good Idea.
        >[/color]

        It might be a convention but its not a good idea. By putting "Student.h"
        first the OP checks that the header does not have any unwanted include order
        requirements of other header files. For instance suppose that "Student.h"
        required the std::string class to be fully defined, but the OP had omitted
        to #include <string> in Student.h. With your suggestion this error would not
        be found.

        Generally speaking when writing some_file.cpp, some_file.h should be the
        first included file.

        john


        Comment

        • John Harrison

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

          > >[color=blue][color=green]
          > >I changed the implementation or cpp file to include the foll:
          > >
          > >#include "Student.h"
          > >#include <string>[/color]
          >
          > It's convention to put the standard headers /first/, and you user-defined
          > headers last. Not absolutely required, but A Good Idea.
          >[/color]

          It might be a convention but its not a good idea. By putting "Student.h"
          first the OP checks that the header does not have any unwanted include order
          requirements of other header files. For instance suppose that "Student.h"
          required the std::string class to be fully defined, but the OP had omitted
          to #include <string> in Student.h. With your suggestion this error would not
          be found.

          Generally speaking when writing some_file.cpp, some_file.h should be the
          first included file.

          john


          Comment

          • Leor Zolman

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

            On Sat, 10 Apr 2004 13:25:46 +0100, "John Harrison"
            <john_andronicu s@hotmail.com> wrote:
            [color=blue][color=green][color=darkred]
            >> >
            >> >I changed the implementation or cpp file to include the foll:
            >> >
            >> >#include "Student.h"
            >> >#include <string>[/color]
            >>
            >> It's convention to put the standard headers /first/, and you user-defined
            >> headers last. Not absolutely required, but A Good Idea.
            >>[/color]
            >
            >It might be a convention but its not a good idea. By putting "Student.h"
            >first the OP checks that the header does not have any unwanted include order
            >requirements of other header files. For instance suppose that "Student.h"
            >required the std::string class to be fully defined, but the OP had omitted
            >to #include <string> in Student.h. With your suggestion this error would not
            >be found.[/color]

            Okay, I can see the value in that... this sent me scrambling to see how
            often your convention is actually used among the "high end" C++ texts out
            there. The first thing I noticed was that gathering evidence is difficult,
            since books like TC++PL and _Accelerated C++_ simply don't list complete
            source files in the text...so you can't really tell what the authors'
            preferred inclusion order is.

            However, a few of the landmark books, such as "Modern C++ Design", do have
            code associated with them (Loki in the case of that particular book).
            Andrei Alexandrescu /does/ indeed use your convention. That's a serious
            endorsement.

            I think I've just switched my convention.
            Thanks,
            -leor
            [color=blue]
            >
            >Generally speaking when writing some_file.cpp, some_file.h should be the
            >first included file.
            >
            >john
            >[/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

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

              On Sat, 10 Apr 2004 13:25:46 +0100, "John Harrison"
              <john_andronicu s@hotmail.com> wrote:
              [color=blue][color=green][color=darkred]
              >> >
              >> >I changed the implementation or cpp file to include the foll:
              >> >
              >> >#include "Student.h"
              >> >#include <string>[/color]
              >>
              >> It's convention to put the standard headers /first/, and you user-defined
              >> headers last. Not absolutely required, but A Good Idea.
              >>[/color]
              >
              >It might be a convention but its not a good idea. By putting "Student.h"
              >first the OP checks that the header does not have any unwanted include order
              >requirements of other header files. For instance suppose that "Student.h"
              >required the std::string class to be fully defined, but the OP had omitted
              >to #include <string> in Student.h. With your suggestion this error would not
              >be found.[/color]

              Okay, I can see the value in that... this sent me scrambling to see how
              often your convention is actually used among the "high end" C++ texts out
              there. The first thing I noticed was that gathering evidence is difficult,
              since books like TC++PL and _Accelerated C++_ simply don't list complete
              source files in the text...so you can't really tell what the authors'
              preferred inclusion order is.

              However, a few of the landmark books, such as "Modern C++ Design", do have
              code associated with them (Loki in the case of that particular book).
              Andrei Alexandrescu /does/ indeed use your convention. That's a serious
              endorsement.

              I think I've just switched my convention.
              Thanks,
              -leor
              [color=blue]
              >
              >Generally speaking when writing some_file.cpp, some_file.h should be the
              >first included file.
              >
              >john
              >[/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

              • San

                #22
                Re: Trouble with constructor

                Hi,
                While trying to reply to this message, I started a new thread by
                mistake. Sorry about that.

                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


                "Gary Labowitz" <glabowitz@comc ast.net> wrote in message news:<KfmdnSEtl fzrk-rdRVn-vw@comcast.com> ...[color=blue]
                > "Mike Higginbottom" <news@peak41.co .uk> wrote in message
                > news:opr57hwusi ct0wm7@fuzzbox. ..[color=green]
                > > On 9 Apr 2004 12:37:58 -0700, San <abc_99_420@hot mail.com> wrote:
                > >[color=darkred]
                > > > I wrote two simple constructors (one of them default) to[/color][/color]
                > initialize a[color=green][color=darkred]
                > > > student object.[/color]
                > >
                > > [snip][color=darkred]
                > > > 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=green]
                > > 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=green]
                > > };
                > >[/color]
                >
                > All other problems were taken care of.[/color]

                Comment

                • San

                  #23
                  Re: Trouble with constructor

                  Hi,
                  While trying to reply to this message, I started a new thread by
                  mistake. Sorry about that.

                  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


                  "Gary Labowitz" <glabowitz@comc ast.net> wrote in message news:<KfmdnSEtl fzrk-rdRVn-vw@comcast.com> ...[color=blue]
                  > "Mike Higginbottom" <news@peak41.co .uk> wrote in message
                  > news:opr57hwusi ct0wm7@fuzzbox. ..[color=green]
                  > > On 9 Apr 2004 12:37:58 -0700, San <abc_99_420@hot mail.com> wrote:
                  > >[color=darkred]
                  > > > I wrote two simple constructors (one of them default) to[/color][/color]
                  > initialize a[color=green][color=darkred]
                  > > > student object.[/color]
                  > >
                  > > [snip][color=darkred]
                  > > > 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=green]
                  > > 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=green]
                  > > };
                  > >[/color]
                  >
                  > All other problems were taken care of.[/color]

                  Comment

                  Working...