Fortran: error # 6562: initialization of data-expr is not valid for this object

I try to initialize a local logical array in the Fortran routine to false, but I get an error:

error # 6562: initialization of data-expr is not valid for this object.

Here is my expression:

integer , intent(in) :: nLOW integer , intent(in) :: nUP logical , dimension(nLOW:nUP) :: leastSQUARE = .false. 

I get the same error if I use:

  integer :: I integer , intent(in) :: nLOW integer , intent(in) :: nUP logical , dimension(nLOW:nUP) :: leastSQUARE = (/ (.false., I = nLOW:nUP) /) 

If I write:

  integer , intent(in) :: nLOW integer , intent(in) :: nUP logical , dimension(1:100) :: leastSQUARE = .false. 

the routine compiles with zero errors. Any idea why this is happening? I explicitly need leastSQUARE with dimensions nLOW:nUP , so the latter is not a workaround.

+6
source share
1 answer

I believe that building with an error is prohibited by the language standard, in particular (in the version of Fortran 2008), C506 on R503. This restriction contains

Initialization should not occur if the object-name is a dummy argument, the result of the function, the object in the named general block, if only the declaration type is in the data block of the data block, the object in the space is general, a distributed variable or an automatic object.

leastSQUARE is just an automatic object whose boundaries are known only at run time. You will have to initialize it separately from its declaration.

To be clear (thanks @IanH), you will need to execute an assignment statement to give the auto object an initial value. My use of verb initialization in the previous paragraph was not a standard Fortran standard.

+8
source

Source: https://habr.com/ru/post/974205/


All Articles