错误:如果 SELECT TYPE 中的选择器表达式不是命名变量,则 associate-name=>将出现 [英] Error: If selector expression in SELECT TYPE is not a named variable, associate-name=> shall appear

查看:13
本文介绍了错误:如果 SELECT TYPE 中的选择器表达式不是命名变量,则 associate-name=>将出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

错误信息是:

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

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

How can I make this code compile?

推荐答案

没有理由使用指针,只是使用select type的关联部分(错误信息不是你写的,但是IIRC这是非常具有描述性的):

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)

这篇关于错误:如果 SELECT TYPE 中的选择器表达式不是命名变量,则 associate-name=>将出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆