在Fortran中使用派生类型指针和多态目标

时间:2012-03-11 19:46:37

标签: pointers fortran gfortran polymorphism

下面列出的Fortran函数使用ifort 11.1按预期编译和执行。但是,GFortran 4.6返回编译器错误:

  

THIS_NML =>本
  错误:(1)处指针赋值的不同类型;尝试将CLASS(UNIT)分配给TYPE(UNIT)。

Fortran代码:

FUNCTION PROCESS_COMMAND(THIS, CMD, DATA) RESULT(RET)
   CLASS(UNIT), INTENT(INOUT), TARGET :: THIS
   CHARACTER(LEN = *), INTENT(IN)     :: CMD
   CHARACTER(LEN = *), INTENT(IN)     :: DATA
   CHARACTER(LEN = 200)               :: STRING
   INTEGER                            :: IOS
   TYPE(UNIT), POINTER                :: THIS_NML

   ! CREATE A NAMELIST
   NAMELIST /VARS/ THIS_NML
   THIS_NML => THIS  
   RET = 0
   STRING = '&VARS THIS_NML%' // TRIM(CMD) // ' = ' // TRIM(DATA) // ' /'

   ! READ CMD AND DATA
   READ(STRING, NML=VARS, IOSTAT=IOS)
   RET = IOS

END FUNCTION PROCESS_COMMAND

因为无法使用多态对象创建名称列表,所以派生类型指针THIS_NML用于创建名称列表。有关如何使用GFortran的任何想法吗?

1 个答案:

答案 0 :(得分:1)

尝试使用select type构造。像

这样的东西
select type ( A => THIS )
   type is ( UNIT )
      Here you can treat A as nonpolymorphic variable of type UNIT (if the test is succesful).
end select