[comp.lang.c++] Problems with typedef variable init'ing in c++

lombardo@sun1.cs.nps.navy.mil (charles lombardo) (06/26/91)

If I compile the following program with the C compiler, everything works fine.

<>  typedef float Matrix[4][4];
<>  
<>  extern void unit();
<>  
<>  main()
<>  {
<>       Matrix m;
<>  
<>       unit(m);
<>   }    
<>  
<>  
<>  void unit(m)
<>  Matrix m;
<>  {
<>    static Matrix un = { 1.0, 0.0, 0.0, 0.0,
<>  		           0.0, 1.0, 0.0, 0.0,
<>  		           0.0, 0.0, 1.0, 0.0,
<>  		           0.0, 0.0, 0.0, 1.0 };
<>    int i,j;
<>      
<>    /* copy the matrix elements...*/
<>    for(i=0; i < 4; i++) {
<>      for(j=0; j < 4; j++) {
<>        m[i][j] = un[i][j];
<>      }
<>    }
<>  }


The problem is when I compile the same program slightly changed for the C++
compiler.  Namely:

<>  typedef float Matrix[4][4];
<>  
<>  extern void unit(Matrix);
<>  
<>  main()
<>  {
<>       Matrix m;
<>  
<>       unit(m);
<>  }    
<>  
<>  
<>  void unit(Matrix m)
<>  {
<>    static Matrix un = { 1.0, 0.0, 0.0, 0.0,
<>  		           0.0, 1.0, 0.0, 0.0,
<>  		           0.0, 0.0, 1.0, 0.0,
<>  		           0.0, 0.0, 0.0, 1.0 };
<>    int i,j;
<>      
<>    /* copy the matrix elements...*/
<>    for(i=0; i < 4; i++) {
<>      for(j=0; j < 4; j++) {
<>        m[i][j] = un[i][j];
<>      }
<>    }
<>  }

When I compile the above I get the following message:

<>  "tmp2.c", line 7: warning:  m used but not set
<>  "tmp2.c", line 15: error: bad initializer type for un: double 
<>            					 ( float (*)[4] expected)
<>  "tmp2.c", line 15: error:  initializer list too long

Can anybody explain why I cannot initialize the static Matrix variable.
I have gotten around the problem by initializing a static float[4][4]
variable instead, but this leaves a little bit to be desired.  The Matrix
typdef is done in a system include file and it would be nice if these
guys could be initialized as in the code above.


Details:  SGI IRIS 4D Machine  (gl.h contains the typedef for Matrix)
          AT&T C++ 2.0


Any comments are appreciated.

Thanks,  Chuck

 ----------------------------------------------------------------------------
| Charles P. Lombardo			|           lombardo@cs.nps.navy.mil |
| Computer Science Department		|		 voice: 408-646-2102 |
| Naval Postgraduate School		|		 fax:   408-646-2814 |
 ----------------------------------------------------------------------------

steve@taumet.com (Stephen Clamage) (06/27/91)

lombardo@sun1.cs.nps.navy.mil (charles lombardo) writes:
|The problem is when I compile the same program slightly changed for the C++
|compiler.  Namely: [crunched]

|<>  typedef float Matrix[4][4];
|<>  extern void unit(Matrix);
|<>  main()
|<>  {
|<>       Matrix m;
|<>       unit(m);				// line 7
|<>  }    
|<>  void unit(Matrix m)
|<>  {
|<>    static Matrix un = {1.0, 0.0, 0.0, 0.0,	// line 15
|<>  		           0.0, 1.0, 0.0, 0.0,
|<>  		           0.0, 0.0, 1.0, 0.0,
|<>  		           0.0, 0.0, 0.0, 1.0 };
|<>    ...
|<>  }
|When I compile the above I get the following message:
|<>  "tmp2.c", line 7: warning:  m used but not set
|<>  "tmp2.c", line 15: error: bad initializer type for un: double 
|<>            					 ( float (*)[4] expected)
|<>  "tmp2.c", line 15: error:  initializer list too long

The first warning is because Cfront sees an uninitialized array passed
(as a pointer) to a routine, and is trying to be helpful.

The error messages are due to a bug in Cfront; your code is ok, although
I would suggest using fully-bracketed initialization for clarity and
ease of maintenance.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com