class - Fortran convert string to number -


i want have subroutine converts contents of numeric string numeric type (int, real, double precision, real(real128)).

however getting error when trying use class(*). error shown below:

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 @ (1) cannot polymorphic unless  processed defined     input/output procedure lib/larsa.f:1935.32:   read (s, *, iostat=ios) num                             1 error: data transfer element @ (1) cannot polymorphic unless  processed defined input/output procedure 

this subroutine have written.

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 , puts value in i.  if (present (fmt))   frmt = "(" // trim (fmt) //  ")"   read (s, frmt, iostat=ios) num else   read (s, *, iostat=ios) num end if  end subroutine converts_str_to_num 

to tidy comments, i'll provide answer.

the error message clear: cannot have polymorphic variable in input/output list unless list processed defined input/output. 9.6.3.5 in fortran 2008. class(*) num (unlimited) polymorphic.

now, polymorphic derived types define such defined input/output procedure, counts lot of work , gfortran doesn't (yet) support notion. also, can't intrinsic types. these factors mean have deal non-polymorphic variables in input list have.

of course, it's possible use generics avoid polymorphism, alternative (as polymorphic) use select type construct. simplicity, ignore list-directed , explicit format cases:

select type (assoc => num) type (int)    read (s, *, iostat=ios) assoc type (real)    ... type (...) class default    error stop "oh noes!" end select 

i've used associate name in select type address 1 part of confusion. if you've done

select type(num) type (int)   read (s, *, iostat=ios) num end select 

to think "now using num fine: why?" that's because num inside construct not same num outside. crucially, isn't polymorphic exact type matching type is.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -