4

Fortran 2003 モジュールで という型を定義していますが、後で、型のオブジェクトを唯一の引数としてt_savepoint受け取る というサブルーチンのインターフェイスを定義したいと考えています。fs_initializesavepointt_savepoint

モジュール全体のコードは次のとおりです。

module m_serialization

    implicit none

    type :: t_savepoint
        integer :: savepoint_index
        real    :: savepoint_value
    end type t_savepoint

    interface

        subroutine fs_initializesavepoint(savepoint)
            type(t_savepoint) :: savepoint
        end subroutine fs_initializesavepoint


    end interface

end module m_serialization

このようなインターフェースが必要な理由は、後でこの fortran モジュールを C と相互運用できるようにするためです。

これをコンパイルしようとすると (gfortran-4.7.0)、次のエラー メッセージが表示されます。

            type(t_savepoint) :: savepoint
                                          1
Error: The type of 'savepoint' at (1) has not been declared within the interface

サブルーチン内で型の定義を移動すると、エラーは消えます。しかし、多くのサブルーチン内で同じ型を使用したい場合、それらすべてで定義を繰り返す必要がありますか?

前もって感謝します。

EDIT : 解決策は、型の定義を別のモジュールに移動してuseから、すべてのサブルーチンでそれに移動することです。t_savepointただし、型とサブルーチンは同じ概念上のトピックの一部であるため、このソリューションはあまり好きではありません。

4

1 に答える 1

8

正しいか間違っているかにかかわらず、インターフェイス ブロックでは、ホストの関連付けによって環境にアクセスできません。これを修正するには、データ型を明示的にインポートする必要があります。

[luser@cromer stackoverflow]$ cat type.f90
module m_serialization

  implicit none

  type :: t_savepoint
     integer :: savepoint_index
     real    :: savepoint_value
  end type t_savepoint

  interface

     subroutine fs_initializesavepoint(savepoint)
       Import :: t_savepoint
       type(t_savepoint) :: savepoint
     end subroutine fs_initializesavepoint


  end interface

end module m_serialization
[luser@cromer stackoverflow]$ gfortran -c type.f90

f2003です。

しかし、あなたがこれを書いた方法は、あなたがこれを最善の方法でコーディングしようとしていないことを示唆していると思います. 単純にルーチン自体をモジュールに入れる方がよいでしょう。次に、インターフェイスを気にする必要はまったくありません。

module m_serialization

  implicit none

  type :: t_savepoint
     integer :: savepoint_index
     real    :: savepoint_value
  end type t_savepoint

Contains

  Subroutine fs_initializesavepoint(savepoint)

    type(t_savepoint) :: savepoint 

    Write( *, * ) savepoint%savepoint_index, savepoint%savepoint_value

  End Subroutine fs_initializesavepoint

end module m_serialization
[luser@cromer stackoverflow]$ gfortran -c type.f90

モジュールが実際に接続されたエンティティを処理するように設計されていることを考えると、これは実際に Fortran で行う方法です。また、f95 コンパイラのみを必要とするという利点もあるため、どこでも利用できます (確かに import は一般的に実装されています)。

于 2012-12-13T10:45:13.230 に答える