模块过程的名称与包含作用域单元中的名称冲突



我正在学习Fortran子模块,这是我的代码

module name1
implicit none
interface
module subroutine test2(x)
integer,intent(in) :: x
end subroutine test2
end interface
contains
subroutine test1(x)
integer :: x
call test2(x)
end
end module name1

module name2
use name1
implicit none
integer :: z = 22
end module name2

submodule(name1) new
use name2
contains
subroutine test2(x)
integer,intent(in):: x
integer:: y 
y = 2
print *, x+y+z ,'from test2'
end  
end submodule

program name
use name2
implicit none
call test1(5)
end program name

但是当使用ifort (IFORT) 2021.3.0 20210609编译时,它显示以下错误

test.f90(26): error #6645: The name of the module procedure conflicts with a name in the encompassing scoping unit.   [TEST2]
subroutine test2(x)
-------------------^
compilation aborted for test.f90 (code 1)

我不明白我做错了什么。它是不是一个有效的Fortran子模块使用?

在子模块(单独的模块过程)中定义过程的实现时,需要关键字module

代码的相关部分将变成:

module name1
implicit none
interface
module subroutine test2(x)
integer,intent(in) :: x
end subroutine
end interface
end module
submodule(name1) test
module subroutine test2(x) ! Note the keyword `module`
integer,intent(in):: x
integer:: y 
y = 2
print *, x+y+z ,'from test2'
end subroutine
end submodule

您还可以使用module procedure来减少重复代码的数量,如

module name1
implicit none
interface
module subroutine test2(x)
integer,intent(in) :: x
end subroutine
end interface
end module
submodule(name1) test
module procedure test2
integer:: y 
y = 2
print *, x+y+z ,'from test2'
end procedure
end submodule

使用module procedure意味着您不必重复过程(x)或它们的声明integer,intent(in):: x的参数。

最新更新