Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!crdgw1!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: HELP: default constructor for array of class Keywords: constructor, default, class, array, definition Message-ID: <701@taumet.com> Date: 3 May 91 15:46:03 GMT References: <1991May2.165955.28385@iitmax.iit.edu> Organization: Taumetric Corporation, San Diego Lines: 34 chien@iitmax.iit.edu (Greg Chien) writes: >When compiling the following program, C++ complains that Point::Point() >default constructor is not found in resolving Point p[2]. However, >if Point() { x = 200; } is uncommented, ambiguous constructors error >is generated in resolving Point q. >class Point { > ... >public: >// Point() { x = 200; } > Point(int xc = 100) { x = xc; } >}; > Point p[2]; A default constructor is required to initialize the array 'p'. Under the latest language rules, any constructor requiring no parameters (such as 'Point(int=100);') will serve, but this is also noted as a recent extension. With future compilers, the could should work as given. With many current compilers (including Borland C++ 2.0) the 'Point()' constructor is required, as that was the earlier rule. But the combination Point(); Point(int = 100); is ambiguous, since neither constructor needs to be called with any parameters. You can achieve exactly the effect of your original code by using Point() { x = 100; } Point(int xc) { x = xc; } This satisfies both old and new language rules. -- Steve Clamage, TauMetric Corp, steve@taumet.com