Types &引用;派生类型在定义之前正在使用;在接口块中
在Windows上使用cygwin64此程序将不会编译:Types &引用;派生类型在定义之前正在使用;在接口块中,types,fortran,cygwin,operator-keyword,gfortran,Types,Fortran,Cygwin,Operator Keyword,Gfortran,在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) t
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比较陌生,所以这个示例代码可能还有一些错误
出现了相同的问题,但将
myType
封装到单独的模块中并没有解决此问题。您的代码存在多个问题,但此特定错误是因为myType
位于主机作用域中,而不在接口块中。解决方案是按照链接线程中的建议将派生类型放置在单独的模块中,或者从主机作用域单元导入派生类型:
interface operator (>)
logical function compare(a,b)
import myType
type(myType),intent(in) :: a,b
end function compare
end interface operator (>)
Fortran 2008标准第12.4.3.3条“进口声明”对此进行了描述:
1 IMPORT语句指定可以在接口中访问主机作用域单元中的命名实体
由东道主协会主办。以这种方式导入并在主机范围界定单元中定义的实体应为
在接口主体之前显式声明
接口块可能没有包含可执行语句-因此您在其中的赋值无效。此外,
卡
未在代码中定义 接口块仅用于指定使用此运算符调用哪个例程,而不是定义例程本身。因此,您需要在某个地方单独编写compare()
的主体。我相信在一个模块中编写类型定义和相关例程(如compare()
)以及接口操作符(>)是最方便的;程序:比较;结束界面
在那里。e、 在Walter S.Brainerd的《Fortran 2008编程指南》中,它也是这样做的。我已经用这种方式实现了操作符,它现在可以正常工作了!谢谢:)