繁体   English   中英

通过C函数定义的Fortran派生类型构造函数

[英]Fortran derived type constructor defined though C function

我想通过C函数定义派生类型构造函数。 在以下示例中,我设法通过C接口定义了一个重载加法运算符。 尽管语法非常相似,但构造函数的定义在gfortran 4.9下失败,并出现以下错误:

test.f90:7.52:

    module procedure my_module_fortran_new_my_double
                                                    1
Error: 'my_module_fortran_new_my_double' at (1) is not a module procedure

花了一些时间在谷歌上搜索或查看堆栈溢出时的上一篇文章之后,我没有找到任何解决方案。 如果有人能告诉我是什么触发了此错误以及如何纠正该错误,我将感到非常高兴。

这是我模块的源代码:

module my_module
  use iso_c_binding
  type, bind(c) :: my_double
     real(c_double) :: x
  end type my_double
  interface my_double
    module procedure my_module_fortran_new_my_double
  end interface
  interface operator (+)
     module procedure my_module_fortran_add
  end interface operator (+)
  interface
     type(my_double) function my_module_fortran_new_my_double (v) bind ( c )
       use iso_c_binding
       import :: my_double
       real (c_double), intent(in) :: v
     end function my_module_fortran_new_my_double
     type(my_double) function my_module_fortran_add (v1,v2) bind ( c )
       use iso_c_binding
       import :: my_double
       type (my_double), intent(in) :: v1,v2
     end function my_module_fortran_add
  end interface
end module my_module

外部过程不是模块过程。 我认为这是您想要的:

module my_module
  use iso_c_binding
  type, bind(c) :: my_double
     real(c_double) :: x
  end type my_double

    interface
       type(my_double) function my_module_fortran_new_my_double (v) bind ( c )
         use iso_c_binding
         import :: my_double
         real (c_double), intent(in) :: v
       end function my_module_fortran_new_my_double
       type(my_double) function my_module_fortran_add (v1,v2) bind ( c )
         use iso_c_binding
         import :: my_double
         type (my_double), intent(in) :: v1,v2
       end function my_module_fortran_add
  end interface

  interface my_double
   procedure my_module_fortran_new_my_double
  end interface
  interface operator (+)
    procedure :: my_module_fortran_add
  end interface operator (+)

end module my_module

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM