user1407220
user1407220

Reputation: 421

Modules and type bound procedures

I get the following error message:

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

when compiling the source code below with ifort 12.0.3 on a ubuntu 12.04 64 bit platform.

Any Ideas?

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

Upvotes: 1

Views: 1743

Answers (2)

High Performance Mark
High Performance Mark

Reputation: 78316

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.

Upvotes: 1

DaveP
DaveP

Reputation: 7102

You have defined the routine add_sub twice, and the names are clashing with each other. This can be easily solved by adding the following line at the start of module Header:

private add_sub

This makes the definition of add_sub private to the module, so it won't be directly accessible to any routines which import the module - instead they will access it through the public interface add

Upvotes: 0

Related Questions