473,466 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trouble with constructor

San
Hi,

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

}
Student::Student()
{
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::Student(basic_string(char, string_char_traits(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
Jul 22 '05 #1
22 1123
On 9 Apr 2004 12:37:58 -0700, ab********@hotmail.com (San) wrote:
Hi,

I wrote two simple constructors (one of them default) to initialize a
student object.
Student::Student(string name)
{
student_name = name;
student_id = LATEST_ID + 1;
student_address(""); You've confused member initializer syntax with stuff you do elsewhere than
in the initializer list. You want something like this:

Student::Student(string name) : student_name(name),
student_id(LATEST_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

student_phone(0);
student_email("");

}
Student::Student()
{
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::Student(basic_string(char, string_char_traits(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 --- 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:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
On 9 Apr 2004 12:37:58 -0700, ab********@hotmail.com (San) wrote:
Hi,

I wrote two simple constructors (one of them default) to initialize a
student object.
Student::Student(string name)
{
student_name = name;
student_id = LATEST_ID + 1;
student_address(""); You've confused member initializer syntax with stuff you do elsewhere than
in the initializer list. You want something like this:

Student::Student(string name) : student_name(name),
student_id(LATEST_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

student_phone(0);
student_email("");

}
Student::Student()
{
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::Student(basic_string(char, string_char_traits(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 --- 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:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3
"San" <ab********@hotmail.com> wrote in message
news:ba**************************@posting.google.c om
Hi,

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

}
Student::Student()
{
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::Student(basic_string(char, string_char_traits(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


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)

Jul 22 '05 #4
"San" <ab********@hotmail.com> wrote in message
news:ba**************************@posting.google.c om
Hi,

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

}
Student::Student()
{
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::Student(basic_string(char, string_char_traits(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


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)

Jul 22 '05 #5
On 9 Apr 2004 12:37:58 -0700, San <ab********@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;
};

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

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

Student stud_1("San");
The compiler has no problem if I use Student stud_1();
I'd expect it to fail on:

student_name("");
Is the trouble due to the fact that I am trying to assign a string and
that there is no copy constructor defined? No. The default copy ctor will work fine for this class and will just
call the copy ctor for std::string which does exist.

Any help would be greatly appreciated.

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::Student(std::string name) : student_name(name)
{
}

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
http://www.peak41.co.uk
Jul 22 '05 #6
On 9 Apr 2004 12:37:58 -0700, San <ab********@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;
};

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

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

Student stud_1("San");
The compiler has no problem if I use Student stud_1();
I'd expect it to fail on:

student_name("");
Is the trouble due to the fact that I am trying to assign a string and
that there is no copy constructor defined? No. The default copy ctor will work fine for this class and will just
call the copy ctor for std::string which does exist.

Any help would be greatly appreciated.

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::Student(std::string name) : student_name(name)
{
}

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
http://www.peak41.co.uk
Jul 22 '05 #7
On Sat, 10 Apr 2004 06:17:33 +1000, "John Carson"
<do***********@datafast.net.au> wrote:

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;


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:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #8
On Sat, 10 Apr 2004 06:17:33 +1000, "John Carson"
<do***********@datafast.net.au> wrote:

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;


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:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #9
On Fri, 09 Apr 2004 20:32:06 GMT, Leor Zolman <le**@bdsoft.com> wrote:
Student stud_1;


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


Not just me then ;)
--
Mike Higginbottom
http://www.peak41.co.uk
Jul 22 '05 #10
On Fri, 09 Apr 2004 20:32:06 GMT, Leor Zolman <le**@bdsoft.com> wrote:
Student stud_1;


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


Not just me then ;)
--
Mike Higginbottom
http://www.peak41.co.uk
Jul 22 '05 #11
"Mike Higginbottom" <ne**@peak41.co.uk> wrote in message
news:opr57hwusict0wm7@fuzzbox...
On 9 Apr 2004 12:37:58 -0700, San <ab********@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.
--
Gary
Jul 22 '05 #12
"Mike Higginbottom" <ne**@peak41.co.uk> wrote in message
news:opr57hwusict0wm7@fuzzbox...
On 9 Apr 2004 12:37:58 -0700, San <ab********@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.
--
Gary
Jul 22 '05 #13
San
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
Jul 22 '05 #14
San
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
Jul 22 '05 #15
On 9 Apr 2004 22:13:13 -0700, ab********@hotmail.com (San) wrote:
//-----------------------------------------------------------------------------


Hi,
Looking better. Here are some tips:
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>
It's convention to put the standard headers /first/, and you user-defined
headers last. Not absolutely required, but A Good Idea.

namespace stud{

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

Student::Student()
{
student_name = "";
student_id = LATEST_ID + 1;
Same story with LATEST_ID and student_phone.
}
} //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.
Hope my earlier explanation of that made enough sense.

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){}
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.


Thanks for all the advice. The Student stud_1(); thing was really
silly of me :-)
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 ;-)


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.
Try Agent. There's Free Agent, but the full-blown version is well worth the
money.
-leor

Thanks,
Santosh


--
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:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #16
On 9 Apr 2004 22:13:13 -0700, ab********@hotmail.com (San) wrote:
//-----------------------------------------------------------------------------


Hi,
Looking better. Here are some tips:
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>
It's convention to put the standard headers /first/, and you user-defined
headers last. Not absolutely required, but A Good Idea.

namespace stud{

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

Student::Student()
{
student_name = "";
student_id = LATEST_ID + 1;
Same story with LATEST_ID and student_phone.
}
} //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.
Hope my earlier explanation of that made enough sense.

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){}
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.


Thanks for all the advice. The Student stud_1(); thing was really
silly of me :-)
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 ;-)


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.
Try Agent. There's Free Agent, but the full-blown version is well worth the
money.
-leor

Thanks,
Santosh


--
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:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #17
> >
I changed the implementation or cpp file to include the foll:

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


It's convention to put the standard headers /first/, and you user-defined
headers last. Not absolutely required, but A Good Idea.


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
Jul 22 '05 #18
> >
I changed the implementation or cpp file to include the foll:

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


It's convention to put the standard headers /first/, and you user-defined
headers last. Not absolutely required, but A Good Idea.


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
Jul 22 '05 #19
On Sat, 10 Apr 2004 13:25:46 +0100, "John Harrison"
<jo*************@hotmail.com> wrote:
>
>I changed the implementation or cpp file to include the foll:
>
>#include "Student.h"
>#include <string>
It's convention to put the standard headers /first/, and you user-defined
headers last. Not absolutely required, but A Good Idea.


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.


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

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

john


--
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:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #20
On Sat, 10 Apr 2004 13:25:46 +0100, "John Harrison"
<jo*************@hotmail.com> wrote:
>
>I changed the implementation or cpp file to include the foll:
>
>#include "Student.h"
>#include <string>
It's convention to put the standard headers /first/, and you user-defined
headers last. Not absolutely required, but A Good Idea.


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.


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

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

john


--
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:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #21
San
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" <gl*******@comcast.net> wrote in message news:<Kf********************@comcast.com>...
"Mike Higginbottom" <ne**@peak41.co.uk> wrote in message
news:opr57hwusict0wm7@fuzzbox...
On 9 Apr 2004 12:37:58 -0700, San <ab********@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.

Jul 22 '05 #22
San
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" <gl*******@comcast.net> wrote in message news:<Kf********************@comcast.com>...
"Mike Higginbottom" <ne**@peak41.co.uk> wrote in message
news:opr57hwusict0wm7@fuzzbox...
On 9 Apr 2004 12:37:58 -0700, San <ab********@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.

Jul 22 '05 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacek Dziedzic | last post by:
Hi! First of all, I hope my problem is not too loosely tied to the "standard C++" that is the topic of this group. I have some code that exhibits a strange behaviour: on one computer, where I...
22
by: San | last post by:
Hi, I wrote two simple constructors (one of them default) to initialize a student object. Student::Student(string name) { student_name = name; student_id = LATEST_ID + 1;...
6
by: DJ | last post by:
I am currently having trouble with strings. I have a private string variable in my class and i assign a value to it in the creation function as follows. #include <string> using std::string; ...
8
by: Exits Funnel | last post by:
Hello, I've been tasked with porting some C++ code from Windows to Linux. The following excerpt is giving me trouble: //BEGIN CODE #include <string> class TempTestBase_t {
14
by: Gregory L. Hansen | last post by:
I can't seem to make a queue of objects, using the STL queue. I'm trying to make a little event manager, and I just want someplace to store events. The method definitions for EventManager have...
3
by: James | last post by:
I have a base class that has constructor Person(string name, int age) and a derived class Empolyee(string job_title, int salary) When I try to call it using new Employee(name, age, job_title,...
4
by: ingoweiss | last post by:
Hi, I am having trouble passing parameters of a Javascript subclass constructor through to it's superclass constructor. I am trying all sorts of things, including the below, but nothing...
2
by: bellerophon | last post by:
I am having a serious trouble with my pointers in a programm that uses dll and was hoping that somebody in this group could help me: I export my DLL classes with declspec(dllexport) and link...
9
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.