3
在Windows上使用cygwin64这一计划不会编译:在接口块
program test
implicit none
!define my type
type myType
real::foo
integer::bar
end type myType
!define an operator for this type
interface operator (>)
logical function compare(a,b)
type(myType),intent(in) :: a,b
compare = a%foo>b%foo
end function compare
end interface operator (>)
!simple example of operator usage
type(myType) :: tfoo, tbar
tfoo = card(1.,2); tbar = card(3.,4)
print*, tfoo>tbar
end program test
gfortran
(唯一参数是“STD = F2008”)告诉我:
type(myType),intent(in) :: a,b
1
Error: Derived type ‘mytype’ at (1) is being used before it is defined
这让我感到困惑,因为类型是在操作员面前定义的。我对Fortran来说比较新,所以这个示例代码可能会有更多的错误。
发生同样的问题here,但封装myType
在一个单独的模块不能解决问题。
接口块仅用于指定使用此运算符调用哪个例程而不是定义例程本身。所以你需要在别处分别写入'compare()'的主体。我相信在模块中写入类型定义和相关的例程(比如'compare()')和接口操作符(>)是最方便的。 procedure :: compare;在那里结束接口。例如[本页](https://www.ibm.com/developerworks/community/blogs/b10932b4-0edd-4e61-89f2-6e478ccba9aa/entry/object_oriented_fortran_does_fortran_support_operator_overloading55?lang=en) – roygvib
在“Fortran 2008编程指南”中,通过Walter S. Brainerd,它以相同的方式完成。我已经用这种方式实现了运营商,现在它工作的很好!谢谢 :) – Obay