问题
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