gfortran v4.9错误在多态派生类型中使用私有语句

时间:2014-05-28 14:35:48

标签: fortran gfortran polymorphism derived-types

当在用于形成多态对象的派生类型类中使用私有语句时,我在使用gfortran v4.9.0编译器时遇到编译器错误。相反,ifort v14.0.2确实成功编译了此代码,并且可执行文件运行时没有错误。下面的示例源代码导致来自gfortran的以下错误消息:

  

ch2605.poly.f90:58.2:

     

s%x = shape_type(3)

     

1

     

错误:在(1)处分配给可分配的多态变量不是   尚未支持

如果私有语句被注释掉并且整个shape_module属性/方法被公开,那么gfortran会成功编译代码。我的两个问题是:

1)在这种情况下,上面的gfortran错误消息是否正确?

2)尝试将基本派生类型shape_type的内容设为私有时,我应该做些什么?

注意:我已经在fortranplus网站上修改了ch2605_2.f90的示例源代码。

! Test to demonstrate problem private attributesin polymorphic 
! derived type using gfortan v4.9.0 (works OK in ifort v14.0.2)

module shape_module
  implicit none
  private       ! *** Commenting out this statement works with gfortran v4.9.0
  type shape_type
    integer :: n_ 
    character(len=2), allocatable, dimension(:) :: cname_  
  contains
    procedure, pass(this) :: get_size
  end type shape_type
  interface shape_type
    module procedure shape_type_constructor
  end interface
  interface assignment (=)
    module procedure generic_shape_assign
  end interface
  public :: shape_type, generic_shape_assign
  public :: shape_type_constructor, get_size ! *** Not even this works.
contains

  type (shape_type) function shape_type_constructor(n)
    implicit none
    integer, intent (in) :: n
    shape_type_constructor%n_ = n
    allocate ( shape_type_constructor%cname_(n) ) 
  end function shape_type_constructor

  integer function get_size(this)
    implicit none
    class (shape_type), intent (in) :: this
    get_size = size(this%cname_)
  end function get_size

  subroutine generic_shape_assign(lhs,rhs)
    implicit none
    class (shape_type), intent (out), allocatable :: lhs
    class (shape_type), intent (in) :: rhs
    allocate (lhs,source=rhs)
  end subroutine generic_shape_assign
end module shape_module


module shape_wrapper_module
  use shape_module
  type shape_wrapper
  class (shape_type), allocatable :: x
  end type shape_wrapper
end module shape_wrapper_module


program ch2605_mod
  use shape_module
  use shape_wrapper_module
  implicit none
  type (shape_wrapper) :: s
  s %x = shape_type(3)
  print *, 'size = ', s%x%get_size()
end program ch2605_mod

0 个答案:

没有答案