不同文件中的OOP Fortran,类型和过程 [英] OOP Fortran, Type and Procedures in different files

查看:68
本文介绍了不同文件中的OOP Fortran,类型和过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将实际的子例程放在类型绑定过程和类型定义后面的不同文件中.EG:

I was wondering whether it is possible to place the actual subroutines behind type bound procedures and the type definition in different files. EG:

文件A:

Module TypeDef  
Type :: Test  
  Integer :: a,b,c  
contains  
  Procedure, Pass, Public :: add => SubAdd  
End Type  
Type(Test) :: Test  
  Interface  
   Subroutine SubAdd(this)  
      Import TypeDef  
      Class(TypeDef), Intent(InOut) :: this  
  End Subroutine  
  End Interface  
End Module  

文件B:

Module TypeRoutines  
   use TypeDef  
   Private :: SubAdd
contains  
   Subroutine SubAdd(this)  
      Class(TypeDef), Intent(InOut) :: this  
      this%c=this%a+this%b  
   End Subroutine  
End Module  

这不起作用,因为先编译文件A然后再编译文件B时, ifort 会给出错误消息:

This does not work because when compiling file A first and then file B, ifort gives the error message :

The name of the module procedure conflicts with a name in the encompassing scoping unit

这的主要原因是,对于某些类型,我必须编写许多类型绑定过程,并且某些文件正在扩展成百上千行,这使得工作非常繁琐.最终目标是将每个子例程放入一个不同的文件中.

The main reason this is that for some types I have to write lots of type bound procedures, and some files are expanding over hundreds of lines which makes work very tedious. The ultimate goal would be to place every single Subroutine into a different file.

有什么想法吗?

推荐答案

假定您的绑定具有传递的参数(如果没有,则更简单):

Assuming that your bindings have a passed argument (if not, things are easier):

容易!将过程的主体放置在TypeDef模块的子模块中,然后将过程的接口复制并编辑到模块本身的规范部分中的单独的接口主体中.唯一的麻烦是它需要一个F2008编译器(一个支持子模块的编译器),而当前的ifort并不是其中的一个...

Easy! Place the body of the procedures into submodules of the TypeDef module and copy and edit the interface of the procedures into separate interface bodies in the specification part of the module itself. The only hitch is that it requires a F2008 compiler (one that supports submodules) and current ifort isn't one of those...

在现实世界中,我认为您只有以下选择:

In the real world I think you only have the following options:

  • 绑定可以是带有显式接口的外部过程...因此,使实现特定绑定的每个过程成为一个外部过程(不是您尝试的模块过程),为规范中的每个外部过程创建一个接口主体模块的一部分(如您所完成的),然后将每个绑定与其各自的外部过程相关联.如果该类型具有外部过程需要访问的私有组件或绑定,并且需要注意确保接口主体与外部过程的实际接口匹配,则此方法将失败.

  • 将每个模块过程放在单独的文件中,然后在模块的CONTAINS语句之后包含这些单独的文件.

  • 带有大文件.

这篇关于不同文件中的OOP Fortran,类型和过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆