Compiler errors 错误:如果选择类型中的选择器表达式不是命名变量,则关联名称=>;将出现
我试图在另一种类型中使用一种类型。然而,我就是不能让它编译。这对我来说很奇怪:select类型在主程序中起作用,但在子例程中不起作用Compiler errors 错误:如果选择类型中的选择器表达式不是命名变量,则关联名称=>;将出现,compiler-errors,fortran,fortran2003,Compiler Errors,Fortran,Fortran2003,我试图在另一种类型中使用一种类型。然而,我就是不能让它编译。这对我来说很奇怪:select类型在主程序中起作用,但在子例程中不起作用 module ModBuffer implicit none private type, abstract, public :: Buffer contains procedure, public :: Constructor endtype Buffer
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
我在选择类型(此%WF)
行中遇到编译错误。但是如果我在主程序中定义一个缓冲区类型并执行相同的操作,就不会有错误
错误消息是:
error #8253: If selector expression in SELECT TYPE is not a named variable, associate-name=> shall appear.
我怎样才能编译这段代码?我不知道为什么这个主题会被否决。但我找到了解决办法。顺便说一句,我在windows上使用IVF(我已经好几个月没有更新了) 似乎我不能在“select type”子句中使用类型的成员来触发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
没有理由使用指针,只需使用
选择类型的关联部分即可(您没有编写错误消息,但IIRC它非常具有描述性):
我个人不会投票否决这一点,但有些人对“不起作用”和“我有一个错误”过敏,但没有具体说明它如何不起作用以及错误看起来如何。没有人可能会投票反对real(8)
,尽管这不是一个很好的做法。同意弗拉基米尔F的观点,我还要补充一点:有很多代码,这真的是最简单/最普遍的问题吗?你说这是一个修改:什么是修改?确切的错误信息也很有用:我们希望在其他项目中有相同问题的人能够找到答案。对于那些知道答案的人来说,这也是一个有用的提示。@VladimirF,请你再解释一下“没有人可能会投票反对real(8)”。我认为real(8)是“双精度”的替代品。我知道最好用一个变量来指定“种类”。请多告诉我一点你的意见。谢谢。请参阅和kind=8
在不同的平台上可能有不同的含义。便携式解决方案使用select\u real\u kind
或iso\u fortran\u env
或iso\u c\u binding
。太棒了!工作起来很有魅力。
select type (twf => this%WF)