Re: Trouble with constructor

From: San (abc_99_420_at_hotmail.com)
Date: 04/10/04


Date: 10 Apr 2004 07:22:51 -0700

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::string 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::Student(std::string name)
{
 student_name = name;
 student_id = LATEST_ID + 1;
}

Student::Student()
{
        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::Student():student_name(""),student_phone(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@comcast.net> wrote in message news:<KfmdnSEtlfzrk-rdRVn-vw@comcast.com>...
> "Mike Higginbottom" <news@peak41.co.uk> wrote in message
> news:opr57hwusict0wm7@fuzzbox...
> > On 9 Apr 2004 12:37:58 -0700, San <abc_99_420@hotmail.com> wrote:
> >
> > > I wrote two simple constructors (one of them default) to
> initialize a
> > > student object.
> >
> > [snip]
> > > When I try to use the following:
> > > Student stud_1("San");
> > > The g++ compiler gives an error message "undefined message to
> > > Student::Student(basic_string(char, string_char_traits(char),
> > > _default_alloc_template(0,0)))"
> > It would be nice to see the class declaration but the following
> works fine
> > on .NET:
> >
> > class Student
> > {
> > public:
> > Student();
> > Student(std::string name);
> > private:
> > std::string student_name;
>
> I'd like to see if OP declared student_name that way. I'm thinking it
> was a c-style string.
>
> > };
> >
>
> All other problems were taken care of.



Relevant Pages