Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!apple!netcom!teda!ditka!mcdchg!tellab5!balr!clrcom!rmartin From: rmartin@clear.com (Bob Martin) Newsgroups: comp.lang.c++ Subject: Re: Array of class objects Keywords: array, initialization, class objects Message-ID: <1990Nov15.050358.16043@clear.com> Date: 15 Nov 90 05:03:58 GMT References: <1756@enuxha.eas.asu.edu> Organization: Clear Communications, Inc. Lines: 52 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. |