Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

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

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 subroutine the type.

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

I got a compile error in select type(this%WF) line. But if I define a Buffer type in the main program and do the same thing, there would be no error.

The error message is:

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

How can I make this code compile?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No reason to use a pointer, just use the association part of select type (you did not write the error message, but IIRC it is quite descriptive):

   select type (twf => this%WF)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...