当类型名称重载为构造函数时,找不到fortran用户定义的类型

时间:2014-10-27 07:25:57

标签: oop constructor fortran

我在这样的文件中写了一些代码

module ModuleBasicGeometry4
    ...
    type TPoint
      ...
    end type TPoint

    interface TPoint
      module procedure :: TPointInit1,TPointInit2
    end interface TPoint

    contains
        ...
end module ModuleBasicGeometry4

在另一个文件中,我想使用我的用户定义派生类型TPoint。我刚刚使用了use语句:

... 
use ModuleBasicGeomentry4
type(TPoint) :: ...
...

然而,当我使用ifort2013_sp1.3.174编译这些文件时,它告诉我TPoint不是派生类型名称,如果我删除了第一个文件中的interface语句,一切正常。似乎interface语句掩盖了类型语句,因为它们具有相同的名称。更奇怪的是,我在同一个第一个文件中定义了许多其他派生类型和相应的接口作为它们的构造函数,它们都可以正常工作。那么是什么导致了这个奇怪的问题?

P.S。 我想我找到了一些原因,但我不知道为什么。我不是说其他​​类型的工作正常。在第二个文件中,由于我需要一些过程指针,我写了类似这样的东西

...
interface
  ... function ...
      use MoudleBasicGeometry4
      ...
  end function ...
end interface
...

我发现只有在此接口语句之前使用的那些类型才能正常工作。只要在此接口语句之后使用第一个文件中定义的类型,ifort编译器就会给出错误消息:"这不是派生类型名称。"更重要的是,如果我在上面的语句中删除了使用ModuleBasicGeometry4语句,那么evetything也行。有人可以解释原因并告诉我如何解决这个问题吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

从Fortran 2003开始,您可以使用IMPORT语句来导入可在界面外部访问的实体的USE语句,这样您就可以替换在这种情况下似乎有问题的module ModuleBasicGeomentry4 implicit none type TPoint integer :: a integer :: b end type TPoint interface TPoint module procedure :: TPointInit1,TPointInit2 end interface TPoint contains function TPointInit1(a) result(point) integer, intent(in) :: a type(TPoint) :: point point%a = a point%b = a end function function TPointInit2(a,b) result(point) integer, intent(in) :: a,b type(TPoint) :: point point%a = a point%b = b end function end module ModuleBasicGeomentry4 语句

我尝试重现您的代码,然后使用gfortran-4.8正确编译。

MODULE:

PROGRAM main
  use ModuleBasicGeomentry4, only : Tpoint
  implicit none

  type(Tpoint) :: point1
  interface
     integer function foo(point)
        import Tpoint
        type(Tpoint)     :: point
     end function
  end interface
  type(Tpoint)   :: point2  

  point1 = Tpoint(1)
  point2 = Tpoint(1,2)

  print*, foo(point1)
  print*, foo(point2)
END PROGRAM main

integer function foo(point)
  use ModuleBasicGeomentry4 , only : Tpoint
  type(Tpoint)     :: point
  foo = point%a + point%b
end function

MAIN:

{{1}}
相关问题