问题描述
似乎Fortran 90不允许在派生数据类型中使用命名常量.这是真的?以下代码不起作用.
It seems Fortran 90 does not allow named constants in derived data types. Is this true?The following code does not work.
program my_prog
implicit none
type :: my_type
integer, parameter :: a = 1
real(kind(1.d0)) :: b
end type my_type
type (my_type) :: complex_type
end program my_prog
编译器说派生类型定义中不允许使用参数语句.
The compiler says parameter statement is not permitted in derived type definitions.
当我删除 parameter
关键字时,一切正常.但是,如何确定组件 a
在其他地方没有被修改?
When I remove the parameter
keyword everything works fine. But then how can I make sure that the component a
is not modified elsewhere?
推荐答案
根据标准,不允许使用.对于Fortran 90/95,组件属性说明符只能是 pointer
和 dimension
(第4.4.1节),此外, allocatable
(在Fortran 2003中)(第4.5.3节),以及 codimension
和 contiguous
(对于Fortran 2008)(第4.5.4.1节).
According to the Standard, it is not allowed. The component attribute specifier may only be pointer
, and dimension
for Fortran 90/95 (section 4.4.1), additionally allocatable
in Fortran 2003 (Section 4.5.3), and additionally codimension
, and contiguous
for Fortran 2008 (section 4.5.4.1).
您可以在此处获取文档.
我在 target
说明符中遇到了类似的问题,这也是不允许的.
I ran into a similar problem with the target
specifier, that is also not allowed.
为什么不尝试 private
组件?
module typedef
type :: my_type
integer, private :: a_int = 1
real(kind(1.d0)) :: b
contains
procedure :: a
end type my_type
contains
function a(complex_type)
class(my_type),intent(in) :: complex_type
integer :: a
a = complex_type%a_int
end function
end module
program my_prog
use typedef
implicit none
type (my_type) :: complex_type
complex_type%b = 2.d0 ! This should work
write(*,*) complex_type%a(), complex_type%b
! complex_type%a_int = 3 ! This should fail
end program my_prog
这篇关于将常量命名为派生数据类型的组成部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!