[comp.lang.c++] Array of class objects

hocker@enuxha.eas.asu.edu (Charles C. Hocker) (11/14/90)

Hello,

	I am having touble trying to created an array of objects of 
class pnt using TC++.  My code is as follows:

class pnt {
	public:
		int X, Y, Z;
		pnt (int x = 0, int y = 0, int z = 0) {X=x; Y=y; Z=z;}
};

class	pnts {
	public:
		int Count;
		pnt Pnts [MAX_PNTS];  // pnt from above
		pnts (int count = 0) {Count = count;}
};

int	main (void)
{
	pnts	Points;
// ...

}

I recieve an error to the effect "cannot find pnt::pnt()" when I compile my
program.  I have tried to use several different methods for getting the 
code segment to work and have found not that will work.  The ARM gives a 
reference on page 289, section 12.6.1, but I still recieve the error
about "pnt::pnt() not found" when I use:
	
	pnt Pnts [MAX_PNTS] = {pnt ()}

Although the ARM talks of initializing the array of objects, it talks
about not being able to initialize an array of objects with constructors.


	Sincerly
		Charles C. Hocker
		hocker@enuxha.eas.asu.edu

rmartin@clear.com (Bob Martin) (11/15/90)

In article <1756@enuxha.eas.asu.edu> hocker@enuxha.eas.asu.edu (Charles C. Hocker) writes:
>
>Hello,
>
>	I am having touble trying to created an array of objects of 
>class pnt using TC++.  My code is as follows:
>
>class pnt {
>	public:
>		int X, Y, Z;
>		pnt (int x = 0, int y = 0, int z = 0) {X=x; Y=y; Z=z;}
>};
>
>class	pnts {
>	public:
>		int Count;
>		pnt Pnts [MAX_PNTS];  // pnt from above
>		pnts (int count = 0) {Count = count;}
>};
>
>int	main (void)
>{
>	pnts	Points;
>// ...
>
>}
>
>I recieve an error to the effect "cannot find pnt::pnt()" when 
>I compile my program.  

I'll take a stab at answering this.  My manual (which happens to be
the version of the ARM published by SUN) says in 12.1 that:

	"A default constructor for a class X is a constructor of the
	form X::X().  A default constructor will not be generated for
	a class X if any constructor has been declared for class X.

Now, since you declared pnt::pnt(x,y,z).  The compiler did _NOT_
generate the default constructor pnt::pnt().  Yet it needs to use
the pnt::pnt() constructor when constructing the array in the pnts
class.  Note that the array cannot be constructed with pnt(x,y,z)
since the compiler has no way of knowing what values should be
assigned to x, y or z.

So the solution is for you to declare and implement a pnt::pnt()
constructor.  Perhaps it should set the coordinates to zero...

-- 
+-Robert C. Martin-----+:RRR:::CCC:M:::::M:| Nobody is responsible for |
| rmartin@clear.com    |:R::R:C::::M:M:M:M:| my words but me.  I want  |
| uunet!clrcom!rmartin |:RRR::C::::M::M::M:| all the credit, and all   |
+----------------------+:R::R::CCC:M:::::M:| the blame.  So there.     |

miron@fornax.UUCP (Miron Cuperman) (11/24/90)

rmartin@clear.com (Bob Martin) writes:

>>class pnt {...
>>		pnt (int x = 0, int y = 0, int z = 0) {X=x; Y=y; Z=z;}
>>};
>>I recieve an error to the effect "cannot find pnt::pnt()" when 
>>I compile my program.  

>So the solution is for you to declare and implement a pnt::pnt()
>constructor.  Perhaps it should set the coordinates to zero...

No.  pnt::pnt() is defined, since all the arguments to pnt(int, int, int)
are optional.  It may be that there is a bug in your compiler, or that it
is not up to date.

If not you define pnt::pnt(), then it would be ambigous which version of
pnt::pnt() to call.

mlm@cs.brown.edu (Moises Lejter) (11/26/90)

About the two constructors:
	pnt::pnt( int x = 0, int y = 0, int y = 0 ) {};
	pnt::pnt() {};
Under AT&T C++ 2.0, these were in fact two different constructors, and
in order to initialize an array of pnt's it was necessary to include
the second one.  Under AT&T C++ 2.1, that restriction was lifted, and
the first constructor will be used to initialize arrays of pnt's, if
the second constructor is not supplied.  Under either compiler, I
believe that including both versions of the constructor and then
defining a variable
	pnt P;
will result in a compile-time error on that line, as the compiler will
be unable to disambiguate that call.

						Moises
--
-----------------------------------------------------------------------------
Internet/CSnet:   mlm@cs.brown.edu		BITNET:  mlm@browncs.BITNET
UUCP:    ...!uunet!brunix!mlm			Phone:	 (401)863-7664
USmail:  Moises Lejter, Box 1910 Brown University, Providence RI 02912

dcurtis@crc.ac.uk (Dr. David Curtis) (11/28/90)

In article <MLM.90Nov25233119@hector.cs.brown.edu> mlm@cs.brown.edu writes:
>
>About the two constructors:
>	pnt::pnt( int x = 0, int y = 0, int y = 0 ) {};
>	pnt::pnt() {};
>Under AT&T C++ 2.0, these were in fact two different constructors, and
>in order to initialize an array of pnt's it was necessary to include
>the second one.  Under AT&T C++ 2.1, that restriction was lifted, and
>the first constructor will be used to initialize arrays of pnt's, if
>the second constructor is not supplied.  Under either compiler, I
>believe that including both versions of the constructor and then
>defining a variable
>	pnt P;
>will result in a compile-time error on that line, as the compiler will
>be unable to disambiguate that call.
>
>						Moises

Easy to get round this, just take out the first default argument to the
first constructor:

	pnt::pnt( int x, int y = 0, int z = 0 ) 
            { pnt_x=x; pnt_y=y; pnt_z=z; }

	pnt::pnt() { pnt_x=0; pnt_y=0; pnt_z=0; }

Dave Curtis

Academic Department of Psychiatry,    Janet:       dc@UK.AC.UCL.SM.PSYCH
Middlesex Hospital,                   Elsewhere:   dc@PSYCH.SM.UCL.AC.UK
Mortimer Street, London W1N 8AA.      EARN/Bitnet: dc%PSYCH.SM.UCL@UKACRL
Tel 081-380 9475 Fax 081-323 1459     Usenet: ...!mcsun!ukc!mrccrc!D.Curtis

jimad@microsoft.UUCP (Jim ADCOCK) (12/04/90)

In article <1990Nov15.050358.16043@clear.com> rmartin@clear.com (Bob Martin) writes:
|I'll take a stab at answering this.  My manual (which happens to be
|the version of the ARM published by SUN) says in 12.1 that:
|
|	"A default constructor for a class X is a constructor of the
|	form X::X().  A default constructor will not be generated for
|	a class X if any constructor has been declared for class X.

ACK!  *My* manual [which happens to *the* version of ARM, published
by Addison-Wesley in 1990 ISBN 0-201-51459-1 in an ugly reddish-brown
cover] says:

	"A default constructor for a class X is a constructor of class
	X that can be called without an argument.

		[A default constructor is typically of the form X::X(),
		but a constructor that can be called with no arguments
		because it has default arguments, X::X(int=0), for 
		example, is also a default constructor.]"

My copy of the ANSI-C++ committee working paper X3J16/90-0091 dated 
Oct 23 1990 speaks similarly.

Please do not quote an individual vendor's reference manuals as being
"the" facts on C++.  At best these manuals will tend to lag a year or
so behind the standardizations effort.  At worse, they may contain
gratuitous incompatibilities and/or extensions.

Please quote either directly from Ellis & Stroustrup's ARM [the reddish-brown
book from Addison-Wesley ISBN 0-201-51459-1] or from the ANSI-C++ committee's
working papers.  By so quoting from the language standards, we put pressure
on vendors to make their compilers conform to the standard, rather than 
allow arguments to develop as to who's compiler is "right."

[There is no ARM except *the* ARM.  Get it.  :-]

steve@taumet.com (Stephen Clamage) (12/05/90)

jimad@microsoft.UUCP (Jim ADCOCK) writes:

>Please quote either directly from Ellis & Stroustrup's ARM [the reddish-brown
>book from Addison-Wesley ISBN 0-201-51459-1] or from the ANSI-C++ committee's
>working papers.

Please do not quote from the X3J16 C++ committee working papers, either.
They carry no official weight, and are subject to change without notice.
They reflect items under discussion by the committee, not a language
standard.  (A working paper might be just the opinions of a single person.)
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

rmartin@clear.com (Bob Martin) (12/07/90)

In article <59511@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes:
>In article <1990Nov15.050358.16043@clear.com> rmartin@clear.com (Bob Martin) writes:
>|I'll take a stab at answering this.  My manual (which happens to be
>|the version of the ARM published by SUN) says in 12.1 that:
>|
>|	"A default constructor for a class X is a constructor of the
>|	form X::X().  A default constructor will not be generated for
>|	a class X if any constructor has been declared for class X.
>
>ACK!  *My* manual [...] *the* version of ARM says:
>	"A default constructor for a class X is a constructor of class
>	X that can be called without an argument.
>
>Please do not quote an individual vendor's reference manuals as being
>"the" facts on C++.  [There is no ARM except *the* ARM.  Get it.  :-]

Sorry, point taken.  *the* ARM says in 12.1:
	
	A default constructor is typically of the form X::X() [...]
	A default contructor will be generated for a class X only if
	no constructor has been declared for class X.

-- 
+-Robert C. Martin-----+:RRR:::CCC:M:::::M:| Nobody is responsible for |
| rmartin@clear.com    |:R::R:C::::M:M:M:M:| my words but me.  I want  |
| uunet!clrcom!rmartin |:RRR::C::::M::M::M:| all the credit, and all   |
+----------------------+:R::R::CCC:M:::::M:| the blame.  So there.     |

jimad@microsoft.UUCP (Jim ADCOCK) (12/13/90)

In article <536@taumet.com> steve@taumet.com (Stephen Clamage) writes:
|jimad@microsoft.UUCP (Jim ADCOCK) writes:
|
|>Please quote either directly from Ellis & Stroustrup's ARM [the reddish-brown
|>book from Addison-Wesley ISBN 0-201-51459-1] or from the ANSI-C++ committee's
|>working papers.
|
|Please do not quote from the X3J16 C++ committee working papers, either.
|They carry no official weight, and are subject to change without notice.
|They reflect items under discussion by the committee, not a language
|standard.  (A working paper might be just the opinions of a single person.)

Well, nothing has "official" weight right now.  The ARM seems to be
holding up pretty well, but for example, I'd hate for people to say 
that template and exceptions *aren't* in the language, because the
ARM calls them experimental.  I'd much rather people try to track the
developments of the ANSI-C++ committee.  For now, I think that either 
the ARM or the ANSI-C++ pronouncements [the working papers, for lack
of anything better] are pretty darned good discriptions of what the
language "is."  And I'd hate to C++ to degenerate into multiple 
dialects, either because people feel there is no "official" description
of the language, or because they're working from old references.
The ANSI-C effort showed that nothing "is" until its over, but good efforts
towards standardization were made before the standard became "official."
I claim most of the benefits of standardization come from trying to 
reach a consensus, not from having made that consensus.