Fortran と割り当て可能なユーザー派生型の正しい割り当てについて質問があります。
これが私のコードです:
module polynom_mod
implicit none
type monomial
integer,dimension(2) :: exponent
end type
type polynom
real, allocatable, dimension(:) :: coeff
type(monomial),allocatable, dimension(:) :: monom
logical :: allocated
!recursive type
type(polynom),pointer :: p_dx,p_dy
contains
procedure :: init
procedure :: init_dx
end type
ここで、次のようなことができる型多項式を導出したいと思います。
p%coeff(1)=1.0
p%monom(1)%exponent(1)=2
そして次のようなもの:
p%p_dx%coeff(1)=1.0
p%p_dx%monom(1)%exponent(1)=2
そのため、初期化して型を割り当てることができる init 型にバインドされた手順をいくつか書きました。
contains
function init(this,num) result(stat)
implicit none
integer, intent(in) :: num
class(polynom),intent(inout) :: this
logical :: stat
allocate(this%coeff(num))
allocate(this%monom(num))
this%allocated = .TRUE.
stat = .TRUE.
end function
function init_dx(this,num) result(stat)
implicit none
integer, intent(in) :: num
class(polynom),intent(inout) :: this
logical :: stat
allocate(this%p_dx%coeff(num))
allocate(this%p_dx%monom(num))
this%p_dx%allocated = .TRUE.
stat = .TRUE.
end function
end module
program testpolytype
use polynom_mod
type(polynom) :: p
if(p%init(2)) then
print *,"Polynom allocated!"
end if
if(p%p_dx%init_dx(2)) then
print *,"Polynom_dx allocated!"
end if
プログラムを終了する
これは gfortran 4.6.3 でコンパイルできますが、実行するとセグメンテーション エラーが発生しました。
再帰的な割り当て可能な型を割り当てる方法はありますか?