[comp.lang.c++] complex arrays

rahmeh@uicsgva.UUCP (07/23/87)

                  
	The following program:

		#include	<complex.h>
		main()
		{
			complex a[2];
		}

	causes the C++ compiler to produce the following error
	message:

		line 5: sorry, not implemented: default arguments
		for constructor for vector of class complex

	If you have an explanation please let me know.


	mail address:	rahmeh@polaris.csg.uiuc.edu

crowl@rochester.arpa (Lawrence Crowl) (08/04/87)

In article <3400003@uicsgva> rahmeh@uicsgva.UUCP writes:
>The following program:
>
>    #include <complex.h>
>    main()
>    {
>        complex a[2];
>    }
>
>causes the C++ compiler to produce the following error message:
>
>line 5: sorry, not implemented: default arguments for constructor for vector
>of class complex
>
>If you have an explanation please let me know.

The C++ compiler implements initialization of class instance elements within
an array by passing the address of the class constructor to another routine
which loops though the elements.  Since the looping routine has no knowledge
of the constructor, it requires that the constructor take no parameters.  The
definition of the class complex in complex.h shows one constructor taking two
parameters.  True, the parameters are defaulted, but the constructor still
takes two parameters.  This the source of the error.  This can be fixed by
changing part of the class definition from:

    complex(double r = 0.0, double i = 0.0) { re=r; im=i; }

to:

    complex() { re=0.0; im=0.0; }
    complex(double r, double i = 0.0) { re=r; im=i; }

Keepers of the compiler (or library), can you make this update?
-- 
  Lawrence Crowl		716-275-8479	University of Rochester
		     crowl@cs.rochester.arpa	Computer Science Department
 ...!{allegra,decvax,seismo}!rochester!crowl	Rochester, New York,  14627

mikem@otc.OZ (Mike Mowbray) (08/05/87)

In article <3400003@uicsgva>, rahmeh@uicsgva.UUCP says:
>                   
> 	The following program:
> 
> 		#include	<complex.h>
> 		main()
> 		{
> 			complex a[2];
> 		}
> 
> 	causes the C++ compiler to produce the following error
> 	message:
> 
> 		line 5: sorry, not implemented: default arguments
> 		for constructor for vector of class complex

The problem is that cfront doesn't yet have a good way to pass arbitrary
arguments into a function that initialises arrays of user-defined objects.
The fix involves creating a complex constructor that takes no arguments.
Alter <complex.h> so that there are two constructors instead of one. The
standard version looks like:

    class complex {
	    double  re, im;
	public:
	    complex(double r = 0.0, double i = 0.0)	{ re = r; im = i; }

	    // .....
    };

Change it to:

    class complex {
	    double  re, im;
	public:
	    complex()					{ re = im = 0.0; }
	    complex(double r, double i = 0.0)		{ re = r; im = i; }

	    // .....
    };

The above program then compiles happily.

			Mike Mowbray
		    Systems Development
			|||| OTC ||

		    ACSnet:  mikem@otc.oz
		      UUCP:  {seismo,mcvax}!otc.oz!mikem 
		     Phone:  (02) 287-4104
		     Snail:  GPO Box 7000, Sydney 2001, Australia