Using a member of a derived type as an iteration variable of a do loop

EDIT: This has been noted as a duplicate of the question of using array elements to iterate a do loop. A similar question should be sure, but you can read the answers and not see anything about using array elements not related to the array. Although the logic may be the same, it is in no way explained to those who are not yet aware that the same logic is applied. Therefore, I think this is a separate issue.

This code will not compile under gfortran (version 5.2.0). The compiler returns an "unclassified statement" error, where it is noted:

module stuff

type foo
  integer :: bar
  contains
  procedure :: iterator
end type foo

contains

subroutine iterator(stuff_inst)

class(foo) :: stuff_inst
integer :: local_bar

do stuff_inst%bar=1,10      ! <--- error
  print *, stuff_inst%bar
enddo

end subroutine

end module stuff

This code compiles just fine, and it works fine with the driver program too:

module stuff

type foo
  integer :: bar
  contains
  procedure :: iterator
end type foo

contains

subroutine iterator(stuff_inst)

class(foo) :: stuff_inst
integer :: local_bar

do local_bar=1,10                ! <--- no error
  stuff_inst%bar = local_bar     ! <--- no error
  print *, stuff_inst%bar
enddo

end subroutine

end module stuff

- ? do? , , .

+4

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


All Articles