我想要一个将数字 字符串的内容转换为数值类型(int,real,double precision,real(real128))的子例程。Fortran将字符串转换为数字
但是,当尝试使用Class(*)
时出现错误。错误 如下所示:
gfortran -o build/lib/larsa.o -c -ffree-form -g -J./build/lib lib/larsa.f
lib/larsa.f:1933.35:
Read (s, frmt, iostat=ios) num
1
Error: Data transfer element at (1) cannot be polymorphic unless
it is processed by a defined input/output procedure
lib/larsa.f:1935.32:
Read (s, *, iostat=ios) num
1
Error: Data transfer element at (1) cannot be polymorphic unless
it is processed by a defined input/output procedure
这是我写的子程序。
Subroutine converts_str_to_num &
( &
s, num, &
fmt, wrn &
)
Character (len=*), Intent (in) :: s
Character (len=*), Intent (in), Optional :: fmt
Class (*) :: num
Character (len=*), Intent (inout), Optional :: wrn
Integer :: ios
Character (len=65) :: frmt
!!$ Reads contents of s and puts value in i.
If (Present (fmt)) Then
frmt = "(" // Trim (fmt) // ")"
Read (s, frmt, iostat=ios) num
Else
Read (s, *, iostat=ios) num
End If
End Subroutine converts_str_to_num
你的问题是什么?错误消息非常明确:除非使用定义的I/O,否则不能在I/O列表中使用多态变量。你是在“这是什么意思?”之后? – francescalus 2014-12-05 21:38:31
有没有解决方法,仍然有'类(*)'在那里? – Zeus 2014-12-05 21:39:50
@ChristopherDimech是的,使用'select type'构造并为所有想要处理的类型写一个case。 – casey 2014-12-05 21:40:54