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

The name of the module procedure conflicts with a name in the encompassing scoping unit

我正在学习 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 子模块使用吗?

在子模块(aseparate module procedure)中定义过程的实现时,需要关键字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.