2

我正在尝试在另一种类型中使用一种类型。但是,我就是不能让它编译。这对我来说很奇怪:选择类型的东西在主程序中有效,但在类型的子程序中不起作用。

module ModBuffer
    implicit none
    private
    type, abstract, public :: Buffer
    contains
        procedure, public                       :: Constructor
    endtype Buffer

    type, extends(Buffer), public :: BufferR
        real(8), allocatable, public            :: BufData(:,:,:)
    endtype BufferR

    type, extends(Buffer), public :: BufferI
        complex(8), allocatable, public         :: BufData(:,:,:)
    endtype BufferI

    contains

    subroutine Constructor(this, dim1, dim2, dim3)
        class(Buffer), intent(inout)            :: this
        integer, intent(in)                     :: dim1, dim2, dim3

        select type(this)
        type is(BufferR)
            allocate(this%BufData(dim1, dim2, dim3))
        type is(BufferI)
            allocate(this%BufData(dim1, dim2, dim3))
        endselect
    endsubroutine Constructor
endmodule ModBuffer

module ModSystem
    use ModBuffer
    implicit none
    private
    type, public :: System
        class(Buffer), allocatable, public :: WF
    contains
    endtype System

    type, extends(System) :: NewSystem
    contains
        procedure, public :: Constructor
    endtype NewSystem

    contains

    subroutine Constructor(this, Flag)
        class(NewSystem), intent(inout) :: this
        logical, intent(in) :: Flag

        if(Flag) then
            allocate(BufferR::this%WF)
        else
            allocate(BufferI::this%WF)
        endif
        select type(this%WF)
        type is(BufferR)
            print *, "Buffer is real."
        type is(BufferI)
            print *, "Buffer is complex."
        endselect
    endsubroutine Constructor
endmodule ModSystem


program test
    use ModSystem
    !use Operation
    class(System), allocatable :: s

    allocate(NewSystem::s)
    call s%Constructor(.true.)
endprogram test

我有一个编译错误select type(this%WF)。但是如果我在主程序中定义一个 Buffer 类型并做同样的事情,就不会出错。

错误信息是:

error #8253: If selector expression in SELECT TYPE is not a named variable, associate-name=> shall appear.

我怎样才能使这段代码编译?

4

2 回答 2

6

没有理由使用指针,只需使用关联部分select type(您没有写错误消息,但 IIRC 它很有描述性):

   select type (twf => this%WF)
于 2014-09-27T08:19:35.730 回答
1

我不知道为什么这个话题被否决。但我找到了解决方案。顺便说一句,我在 Windows 上使用 IVF(几个月没有更新)。

似乎我不能在“选择类型”子句中使用类型的成员来触发 IVF 编译器错误。但是如果你设置一个指向成员的指针,一切都会正常工作。这是一个有点连线的指针解决问题,它没有多大意义。

module ModBuffer
    implicit none
    private
    type, abstract, public :: Buffer
    contains
        procedure, public                       :: Constructor
    endtype Buffer

    type, extends(Buffer), public :: BufferR
        real(8), allocatable, public            :: BufData(:,:,:)
    endtype BufferR

    type, extends(Buffer), public :: BufferI
        complex(8), allocatable, public         :: BufData(:,:,:)
    endtype BufferI

    contains

    subroutine Constructor(this, dim1, dim2, dim3)
        class(Buffer), intent(inout)            :: this
        integer, intent(in)                     :: dim1, dim2, dim3

        select type(this)
        type is(BufferR)
            allocate(this%BufData(dim1, dim2, dim3))
        type is(BufferI)
            allocate(this%BufData(dim1, dim2, dim3))
        endselect
    endsubroutine Constructor
endmodule ModBuffer

module ModSystem
    use ModBuffer
    implicit none
    private
    type, public :: System
        class(Buffer), allocatable, public :: WF
    contains
    endtype System

    type, extends(System), public :: NewSystem
    contains
        procedure, public :: Constructor
    endtype NewSystem

    contains

    subroutine Constructor(this, Flag)
        class(NewSystem), intent(inout) :: this
        logical, intent(in) :: Flag
        class(Buffer), pointer :: P

        if(Flag) then
            allocate(BufferR::this%WF)
        else
            allocate(BufferI::this%WF)
        endif
        call SetPointer(P, this%WF)
        select type(P)
        type is(BufferR)
            print *, "Buffer is real."
        type is(BufferI)
            print *, "Buffer is complex."
        endselect
    endsubroutine Constructor

    subroutine SetPointer(MyP, MyA)
        class(Buffer), pointer :: MyP
        class(Buffer), target :: MyA
        MyP => MyA
    endsubroutine SetPointer
endmodule ModSystem


program test
    use ModSystem
    !use Operation
    class(System), allocatable :: s

    allocate(NewSystem::s)
    select type(s)
    type is(NewSystem)
        call s%Constructor(.true.)
    endselect
endprogram test
于 2014-09-27T04:35:49.007 回答