如何在use module, ONLY:
语句的模块之间包含派生数据类型(类型,而不是变量)?
更多说明:在我的module1
中,我定义了一个派生数据类型(我们称之为my_datatype)和一些此数据类型的数据(我们称之为my_data)。在我的module2
我需要使用my_data
。由于我的module1包含许多module2
不需要的过程,因此我想使用ONLY
语句仅包含my_data
。但是,如果不包含数据类型,它将给出错误:
Derive datatype 'my_type' is used before defined at "type(my_type),intent(out)::A"
很明显module2
无法识别my_datatype
中定义的module1
,因为我没有通过它。但是在'use module,only'语句中包含派生类型的语法是什么?我正在使用Fortran 2003.
module step1
implicit none
type my_type
integer::id
integer,dimension(2)::my_data
end type my_type
type(my_type)::A
end module step1
module step2
use step1,only:A
implicit none
contains
subroutine change_A(A)
type(my_type),intent(inout)::A
A%id = 1
A%my_data(1) = 1
A%my_data(2) = 2
end subroutine change_A
end module step2
program test
! program is in a different folder
use step1
use step2
implicit none
call change_A(A)
end program test
答案 0 :(得分:2)
However, it is giving me error "derived datatype is used before it is defined". It looks like module2 does not recognize my_datatype defined in module1.
Well, yeah. Of course module2 does not recognize your datatype, because it's defined in module1 and in your 'use'-statement you say you only want to use the variable my_data. Simply include the datatype in the 'use'-statement, and it will be known in module2
答案 1 :(得分:0)
鉴于您的更新问题,以下是如何实施其他人建议的修复:
(编辑:实施评论中的建议:
)
module step1
implicit none
type my_type
integer::id
integer,dimension(2)::my_data
end type my_type
type(my_type)::A
end module step1
module step2
use step1, only: my_type
implicit none
contains
subroutine change_A(dummy)
type(my_type),intent(inout) :: dummy
dummy%id = 1
dummy%my_data(1) = 1
dummy%my_data(2) = 2
end subroutine change_A
end module step2
program test
! program is in a different folder
use step1, only: A
use step2, only: change_A
implicit none
call change_A(A)
write(*,*) A
end program test
我已将my_type
添加到'use'语句中。