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

后端 未结 2 1612
栀梦
栀梦 2021-01-22 11:42

I am trying to use a type in another type. However, I just cannot make it compile. It is strange to me: the select type thing works in the main program but it doesn\'t work in a

2条回答
  •  花落未央
    2021-01-22 11:53

    I have no idea why this topic is vote down. But I find a solution. BTW, I am using IVF on windows (which I didn't update for several months).

    It seems I cannot use member of a type in "select type" clause for triggering a IVF compiler error. But if you set a pointer to the member, everything works just fine. It is a bit wired a pointer solve the problem, and it is doesn't make much sense.

    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
    

提交回复
热议问题