[comp.lang.c++] BC++ and String class??

<ACPS2924@Ryerson.Ca> (05/12/91)

I'm having a slight problem with BC++.  Following is a short code sample
from my header file.  I somehow keep getting the same error with String.
What am I doing wrong ??

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

#ifndef __DBFIELD_H
#define __DBFIELD_H

#ifndef __STDLIB_H
#include <stdlib.h>
#endif

#ifndef __CLSDEFS_H
#include <clsdefs.h>
#endif

#ifndef __CLSTYPES_H
#include <clstypes.h>
#endif

#ifndef __SORTABLE_H
#include <sortable.h>
#endif

#ifndef __STRNG_H
#include <strng.h>
#endif

class dbField
{
public:

		dbField(String name)
		{
			fieldName = name;
		};

		virtual char* toString(void)
		{
			cerr << "\nSorry but no toString exists for this dbField\n";
			exit(1);
			return (char*) NULL;
		};

private:

		String fieldName;

};

#endif

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

ERROR: Cannot find 'String::String()' to initialize field 'fieldname' ....

But in the example directry.cpp it somehow gets away with it. Why ???????

Peter
+----------------------------------------------------------------------+
acps2924@ryerson.ca ! Who is the  ! If plumbers designed toilets like
                    ! user,pray,  ! software professionals design tools,
                    ! and who is  ! we'd be up to our knees in crap.
                    ! the used ?  !                 - Charles A. Rovira
+----------------------------------------------------------------------+

jamshid@ut-emx.uucp (Jamshid Afshar) (05/13/91)

Hey, a BC++ question that actually belongs in this group.

In article <91131.195732ACPS2924@Ryerson.Ca> ACPS2924@Ryerson.Ca writes:
>I'm having a slight problem with BC++.  Following is a short code sample
>from my header file.  I somehow keep getting the same error with String.
>What am I doing wrong ??
>
>//--------------------------------------------------------------------------
>
>...some #includes deleted...
>#ifndef __STRNG_H
>#include <strng.h>
>#endif
>
class String {	// defined in strng.h
public:
   String(const char*);
   String(const String&);
...};
>
>class dbField {
>public:
>   dbField(String name) 
>	{ fieldName = name; };
>private:
>	String fieldName;
>};
>
>ERROR: Cannot find 'String::String()' to initialize field 'fieldname' ....

The dbField constructor should be:
   dbField(String name)
      : fieldName(name)  // this is the member initialization list
      { }

Remember that all the data members of a class object are constructed
_before_ entering the body of the constructor.  If you do not
explicitly call a data member's constructor using the member init.
list, its default constructor will be called (which String lacks,
hence the error).  Besides, it's good style to initialize data members
using the init. list (it's mandatory if they're const) as opposed to
using assignment in the construtor body.  Almost all my constructors
have nothing between the curly braces.  While I'm dictating style, the
constructor should really be 'dbField(const String& name)'.  Use
|const| because you're not changing 'name'.  Pass by reference so your
program doesn't have to make a temporary String object and so that it
won't 'clip' objects that are of a type derived from String.  I would
guess 95% of the parameters in my code are |const Type&|.

>But in the example directry.cpp it somehow gets away with it. Why ???????

Check it again.  The Directory class constructor does initialize its
String member using String(const char*).

>Peter
>acps2924@ryerson.ca ! Who is the  ! If plumbers designed toilets like
>                    ! user,pray,  ! software professionals design tools,
>                    ! and who is  ! we'd be up to our knees in crap.
>                    ! the used ?  !                 - Charles A. Rovira

To all: when posting code that does not compile, please indicate
what compiler and version you are using, the exact error text, and
mark the line of code that gave the error.  Also, if the code
references a type or a macro, please include their (abbreviated)
definitions.

BTW, I know it's a style thing, but I find those #ifndef wrappers
around includes really annoying.  They don't have enough of an effect
on compilation speed to warrant their use.  This is especially true
for Borland C++ since you can use pre-compiled headers.  Actually,
they are implemented kinda weird (of course, I don't know how others
do it).  BC++ don't 'compile' a .h file, it saves the effects a series
of '#include' statements and uses it when it sees the same series of
'#include's (and all the compiler options are the same).

Finally, a couple of plugs:
To subscribe to the Turbo(Borland) C++ mailing list, send e-mail to
   listserv@ucf1vm.cc.ucf.edu or listserv@ucf1vm.bitnet
containing the line
   subscribe tcplus-l Jane Doe

There's a BC++ bug list at sun.soe.clarkson.edu [128.153.12.3] in
~ftp/pub/Turbo-C++/bug-report and by e-mail:
To: archive-server@sun.soe.clarkson.edu
Subject: send pub/Turbo-C++/bug-report

Jamshid Afshar
jamshid@emx.utexas.edu

s892992@minyos.xx.rmit.oz.au (Bossman) (05/14/91)

ACPS2924@Ryerson.Ca writes:

>I'm having a slight problem with BC++.  Following is a short code sample
>from my header file.  I somehow keep getting the same error with String.
>What am I doing wrong ??

>class dbField
>{
>public:
>		dbField(String name)
>		virtual char* toString(void)
>private:
>		String fieldName;
>};

>ERROR: Cannot find 'String::String()' to initialize field 'fieldname' ....

   Hmm. This one has me stumped also. I checked the example code and it does 
exist like you say, but it makes no sense. If you examine the String class
provided by the class library you will notice that there is not constrcutor
defined with no arguments (String::String()). Thus the error is understandable
since you are attempting to call a non-existant constructor.
 
   I am not very impressed with this class library. The string class is barely
useable, and there seems to be a lot of unnecessary padding in the code. I
have also found some rather blatent errors. In many cases some of the functions
that it purports to contains do not exist (the operator String() in the Date
and Time classes for example). One other thing I noticed, is that when you
initialise and iterator using the InitIterator() member function for the List
class, it does not set up the Restart variable correctly. A call to restart()
for the iterator could put you anywhere!
 
See ya,
       Kendall Bennett