Passing type bound procedures as arguments

拜拜、爱过 提交于 2019-11-27 09:46:38

test_obj%add_integers_up is not a procedure - it is a binding that happens to be to a procedure called add_integers_up. You cannot pass a binding as an actual argument.

If you want to pass the specific procedure that the binding is associated with, then pass the procedure! Hypothetically:

call print_result_of_subroutine(i4, add_integers_up)

But as other posters have noted, in your example code the interface of that procedure does not match the interface of the corresponding dummy argument in print_result_of_subroutine.

If test_obj%add_integers_up referred to an associated procedure pointer component (and the interface for that component matched what was being expected by the print_result_of_subroutine) then things would work as you appear to be expecting.

Note that Fortran 90 does not support type bound procedures (or procedure pointer components) - your code very much requires Fortran 2003.

You didn't show us the exact error message you got, and I didn't try your example myself, but I'm pretty sure the problem is that the interface of the procedure dummy argument doesn't correspond to the interface of the actual argument that is passed to it.

More explicitly, random_subroutine is declared as taking two arguments, while test_obj%add_integers_up takes three arguments; even though one of them functions as the passed-object dummy argument, it still counts as part of the interface of that procedure.

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