1

我收到以下错误消息:

The name of the module procedure conflicts with a name
in the encompassing scoping unit.   [ADD_SUB]

在64 位平台ifort 12.0.3上编译下面的源代码时。ubuntu 12.04

有任何想法吗?

Module Header

  Type :: hello

      Integer :: a
      Integer :: b    
      Integer :: sum

    contains

      procedure, pass :: add => add_sub

  End type hello

  Interface

    Subroutine add_sub(this)
       Import hello
       Implicit None
       class(hello) :: this

    End Subroutine add_sub

  End Interface

End Module


Module Routines

  use Header

contains

  Subroutine add_sub(this)    
    Implicit None    
    class(hello), intent(inout) :: this
    this%sum=this%a+this%b    
  End Subroutine

End Module



Program Test

  use Header    
  use Routines

  Implicit None

  Type(hello) :: x

  x%a=1    
  x%b=2

  call x%add()

  write(*,*) x

End Program Test
4

2 回答 2

1

I think that the problem you have is that, because Fortran compilers give all module procedures an explicit interface, the compiler finds two instances of add_sub within the topmost scope of program test.

I've had a look at the Fortran 2003 standard and can't immediately find a rule to forbid what you have done. However, it is unusual. The urge to put routine declarations and definitions in separate compilation units seems to afflict C/C++ programmers a lot more than it does run-of-the-mill Fortran programmers.

If you do want to separate these in your coding I think you have the following options:

  • Put your subroutine definitions into a compilation unit which is not a module. I could, for example, compile your program fragment by removing the module routines and having the subroutine add_sub in a compilation unit of its own.
  • Use the include statement to include the text of the source file in which routines are defined.
  • Wait for compilers to implement Fortran 2008's submodule capabilities.

I don't really see this as a particular problem, I'm one of those run-of-the-mill Fortran programmers who is used to putting the entire definition of type-bound procedures in the same compilation unit as the declarations of the types to which they are bound.

It is possible, mind you, that the Fortran standard does not prohibit you doing what you are trying to do but that the Intel compiler doesn't yet implement the feature, or implements it incorrectly. Why not run this past their tech support people, they're usually pretty good.

于 2012-06-18T13:32:29.267 回答
0

您已经定义了两次例程add_sub,并且名称相互冲突。这可以通过在模块开头添加以下行来轻松解决Header

private add_sub

这使得add_sub模块私有的定义,因此任何导入模块的例程都不能直接访问它 - 相反,它们将通过公共接口访问它add

于 2012-06-18T08:33:41.697 回答