Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29277 Discussions

Fortran, Moduls, type bound procedures

may_ka
Beginner
491 Views

I get the 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 a ifort 12.0.3 compiler on a ubuntu 12.04 64 bit platform

Any Ideas?

Thanks

Note: The specifity here is that the type-bound procedure is defined in a different modul than the typ it self. Although the module environment already specifies the interface, commenting the interface block in Module Header leads to an error message at compile time too.

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Module Header
  Implicit None
  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 Non
  Type(hello) :: x
  x%a=1
  x%b=2
  call x%add()
  write(*,*) x
End Program Test
0 Kudos
1 Reply
Arjen_Markus
Honored Contributor II
491 Views
If you want to use add_sub as defined in the module Routines as a specific implementation of the add
method, you will have to do that differently:

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Module Header

Implicit None
type, abstract :: hello
Integer :: a
Integer :: b
Integer :: sum
contains
procedure(add_sub), pass, deferred :: add
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

type, extends(hello) :: hithere
contains
procedure, pass :: add => add_sub_hi
end type

contains
Subroutine add_sub_hi(this)
Implicit None
class(hithere), intent(inout) :: this
this%sum=this%a+this%b
End Subroutine
End Module
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Program Test
use Header
use Routines
Implicit None
Type(hithere) :: x
x%a=1
x%b=2
call x%add()
write(*,*) x
End Program Test

Note the changes:
- type hello is an abstract type now
- add is a deferred method
- I introduced a type hithere that is extended from type hello

While this may not be the simplest way, it has the features you
seem to want:
- An interface, rather than a particular implementation for "add"
- A second module that extends from the first

The code you posted does not connect the interface and the implementation,
so the compiler complained about names that get in each other's way.

Regards,

Arjen
0 Kudos
Reply