"Derived type is being used before it is defined" 在界面块中
"Derived type is being used before it is defined" in interface block
在 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
封装在一个单独的模块中并没有解决问题。
您的代码有几个问题,但这个特定错误是因为 myType
在主机范围内,但不在接口块中。解决方案是按照链接线程中的建议将派生类型放在单独的模块中,或者 import
来自主机范围单元的派生类型:
interface operator (>)
logical function compare(a,b)
import myType
type(myType),intent(in) :: a,b
end function compare
end interface operator (>)
这在 Fortran 2008 标准 Cl 中有描述。 12.4.3.3 "IMPORT statement":
1 The IMPORT statement specifies that the named entities from the host scoping unit are accessible in the interface
body by host association. An entity that is imported in this manner and is defined in the host scoping unit shall be
explicitly declared prior to the interface body.
接口块可能不包含可执行语句 - 因此您在那里的分配无效。此外, card
未在您的代码中定义。
在 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
封装在一个单独的模块中并没有解决问题。
您的代码有几个问题,但这个特定错误是因为 myType
在主机范围内,但不在接口块中。解决方案是按照链接线程中的建议将派生类型放在单独的模块中,或者 import
来自主机范围单元的派生类型:
interface operator (>)
logical function compare(a,b)
import myType
type(myType),intent(in) :: a,b
end function compare
end interface operator (>)
这在 Fortran 2008 标准 Cl 中有描述。 12.4.3.3 "IMPORT statement":
1 The IMPORT statement specifies that the named entities from the host scoping unit are accessible in the interface body by host association. An entity that is imported in this manner and is defined in the host scoping unit shall be explicitly declared prior to the interface body.
接口块可能不包含可执行语句 - 因此您在那里的分配无效。此外, card
未在您的代码中定义。