2016-12-21 7 views
1

派生型コンストラクタをC関数で定義します。次の例では、Cインターフェイスを通じてオーバーロードされた加算演算子を定義することができました。構文は非常に似ているAltough、コンストラクタの定義は次のエラーでのgfortran 4.9の下で失敗します。C関数で定義されたFortran派生型コンストラクタ

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 
+1

'module procedure 'を' procedure'に置き換えるとどうなりますか? – francescalus

+0

さて、手続き本体が(a)モジュールにない場合は、モジュール手続きではありません。モジュール内のプロシージャーへのインターフェースを置いてもそれは変わりません。 –

答えて

2

外部プロシージャは、モジュール手続きではありません。

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 
+0

ありがとうございます。これは機能します。 – Thomas

+0

私はオペレータのオーバーロードしか考慮しないと、モジュールのキーワード(インターネットのどこかにある)でうまく動作する理由を理解できません。かなり矛盾しているようですね。 – Thomas

+0

@Thomas 'module procedure'は、モジュールプロシージャ、つまり実際にモジュールの内部にあるプロシージャのみを参照します。 'procedure'はより一般的で、あなたが明示的なインターフェースを持っている任意の手続きを指します。 –

関連する問題