gfortran 4.9 Generic Type-Bound Operators

青春壹個敷衍的年華 提交于 2020-01-30 12:22:27

问题


I am trying to get generic type-bound operators to work using gfortran 4.9, however I get errors. I have tried using Type(Vector) and Class (Vector) without success.

Type :: Vector

  Real :: x, y, z

Contains

  Procedure :: set => vector_set
  Procedure :: write => vector_write
  generic :: Operator (+) => vector_add

End Type Vector


Function vector_add  &
(                    &
  u, v               &
)                    &
  Result (w)

  !!$ Input
  Type (Vector), Intent(in) :: u, v

  !!$ Output
  Type (Vector) :: w

  w% x = u% x + v% x
  w% y = u% y + v% y
  w% z = u% z + v% z

End Function vector_add

I am getting the following error:

gfortran -o build/lib/foul.o -c -ffree-form -g -J./build/lib lib/foul.f
gfortran -o build/lib/vectors.o -c -ffree-form -g -J./build/lib lib/vectors.f
lib/vectors.f:194.28:

generic :: Operator (+) => vector_add
                        1
Error: Undefined specific binding 'vector_add' as target of GENERIC '+' at (1)
scons: *** [build/lib/vectors.o] Error 1
scons: building terminated because of errors.

回答1:


Inserting the line

procedure :: vector_add

into the contains section of the type definition. As @IanH explains in his comment

Generic bindings resolve to specific bindings (analogous to how generic procedures resolve to specific procedures). If you don't have the line that you recommend inserting, then there is no specific binding (listing a specific binding in the generic statement doesn't define the specific binding - you need an explicit separate type bound procedure statement to do that). The ordering isn't important.

I've promoted his explanation for ease of reading and to make this a better answer than my own now-expunged witchcraft-based explanation.



来源:https://stackoverflow.com/questions/26708064/gfortran-4-9-generic-type-bound-operators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!