Reputation: 430
I am trying to use polymorphism in Fortran, but I have problems allocating the type. I can't figure out how make this work. The Intel compiler threw this error:
error #6460: This is not a field name that is defined in the encompassing structure.
analitic%param%a0=2
---------------^
Here is a minimal example:
program new
implicit none
integer, parameter :: dp = kind(1.0d0)
type :: potential
class(*),allocatable :: param
endtype
type(potential) :: analitic
type :: pa1d_param
real(dp) :: a0
real(dp) :: b0
end type
allocate(pa1d_param::analitic%param)
analitic%param%a0=2.0_dp
end program
What is wrong here?
Thank you!
Upvotes: 0
Views: 1474
Reputation: 6241
By declaring param
as class(*)
, you are declaring an unlimited polymorphic object. These cannot be referenced in a normal way - they can only be used as actual arguments, pointers or targets in pointer assignment, or as selectors in select type
statements (16.3.1, Fortran 95/2003 explained, Metcalf and Reid).
In order to do this as you intended, you will have to declare param
this way: (compiles and produces correct output with ifort 12.0.2.137)
program new
implicit none
integer, parameter :: dp = kind(1.0d0)
type pa1d_param
real(dp) :: a0
real(dp) :: b0
end type pa1d_param
type :: potential
class(pa1d_param),allocatable :: param
endtype
type(potential) :: analitic
allocate(analitic%param)
analitic%param%a0 = 2.0_dp
write(*,*)analitic%param%a0
end program
Upvotes: 2