函数调用中非法使用派生类型
我有一个简单的函数,它基于时间字符串返回一个时间对象:
FUNCTION getTime(timeStr)RESULT(time)
IMPLICIT NONE
CHARACTER(LEN=19),INTENT(IN) :: timeStr
TYPE timeType
INTEGER :: yyyy,mo,dd,hh,mm,ss
ENDTYPE timeType
TYPE(timeType) :: time
READ(UNIT=timeStr( 1: 4),'(I4)')time%yyyy
READ(UNIT=timeStr( 6: 7),'(I2)')time%mo
READ(UNIT=timeStr( 9:10),'(I2)')time%dd
READ(UNIT=timeStr(12:13),'(I2)')time%hh
READ(UNIT=timeStr(15:16),'(I2)')time%mm
READ(UNIT=timeStr(18:19),'(I2)')time%ss
ENDFUNCTION getTime
我从父例程中调用它:
umwmTime1=getTime(umwmStartTimeStr)
umwmTime2=getTime(umwmStopTimeStr)
其中 umwmTime 1 和 2 声明为:
TYPE timeType
INTEGER :: yyyy,mo,dd,hh,mm,ss
ENDTYPE timeType
TYPE(timeType) :: umwmTime1,umwmTime2
我得到的编译错误消息是:
PGF90-S-0099-Illegal use of derived type (ESMF_interface_UMWM.F90: 282)
PGF90-S-0099-Illegal use of derived type (ESMF_interface_UMWM.F90: 283)
0 inform, 0 warnings, 2 severes, 0 fatal for umwm_component_run
第 282 行和 283 行点是函数调用在父例程中。
但是,如果我使用子例程(而不是函数)来获取 umwmTime1 和 umwmTime2 作为 INTENT(OUT) 参数,则不会出现任何问题。我对这个函数做错了什么?
I have a simple function that returns a time object based on a time string:
FUNCTION getTime(timeStr)RESULT(time)
IMPLICIT NONE
CHARACTER(LEN=19),INTENT(IN) :: timeStr
TYPE timeType
INTEGER :: yyyy,mo,dd,hh,mm,ss
ENDTYPE timeType
TYPE(timeType) :: time
READ(UNIT=timeStr( 1: 4),'(I4)')time%yyyy
READ(UNIT=timeStr( 6: 7),'(I2)')time%mo
READ(UNIT=timeStr( 9:10),'(I2)')time%dd
READ(UNIT=timeStr(12:13),'(I2)')time%hh
READ(UNIT=timeStr(15:16),'(I2)')time%mm
READ(UNIT=timeStr(18:19),'(I2)')time%ss
ENDFUNCTION getTime
I call it from the parent routine as:
umwmTime1=getTime(umwmStartTimeStr)
umwmTime2=getTime(umwmStopTimeStr)
where umwmTime 1 and 2 are declared as:
TYPE timeType
INTEGER :: yyyy,mo,dd,hh,mm,ss
ENDTYPE timeType
TYPE(timeType) :: umwmTime1,umwmTime2
The compile error message I get is:
PGF90-S-0099-Illegal use of derived type (ESMF_interface_UMWM.F90: 282)
PGF90-S-0099-Illegal use of derived type (ESMF_interface_UMWM.F90: 283)
0 inform, 0 warnings, 2 severes, 0 fatal for umwm_component_run
Lines 282 and 283 point are function calls in the parent routine.
However if I use subroutine (instead of function) to get umwmTime1 and umwmTime2 as INTENT(OUT) arguments, I get no problems. What am I doing wrong with the function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是编译器不知道您在主程序中定义的时间类型与您在函数中定义的时间类型相同。您应该定义一个位置,最好是在模块中,并让它在任何地方定义类型。
例如,在一个简单的单文件程序中,您提供的代码在 gfortran 中不适用于我,但它可以:
The problem is the compiler doesn't know that the timetype you defined in the main program is the same as the timetype that you defined in the function. You should define that one place, preferably in a module, and let that define the type everywhere.
For instance, in a simple one-file program, the code you provided doesn't work for me in gfortran, but this does:
将函数“getTime”放入模块中,并在调用例程中使用该模块。
问题是调用例程不知道例程函数 getTime 返回 TYPE(timeType)。默认情况下,getTime 被视为实标量值。因此,您需要一个可以通过 USE 语句轻松提供的显式接口。该接口也可以由 INTERFACE 块提供,但不建议这样做,因为容易出错。
我还想指出,参数 timeStr 的声明“CHARACTER(len=19)”是非常危险的。我建议:事实上
,任何使用少于 19 个字符的字符串参数调用该函数都是错误的。
Put you function "getTime" within a module and USE that module in the calling routine.
The problem is that the calling routine does not know that the routine function getTime returns a TYPE(timeType). By default, getTime is considered as a real scalar value. Therefore, you need an explicit interface which is easily provided by a USE statement. This interface may be also provided by an INTERFACE block but this is not advised because error prone.
I would like also to point out that the declaration "CHARACTER(len=19)" of the argument timeStr is very dangerous. I suggest instead :
Indeed any call to that function with a string argument having less that 19 characters is wrong.