Quantcast
Channel: Intel® Fortran Compiler
Viewing all articles
Browse latest Browse all 3270

OOP fortran: can not make a subroutine execute different objects

$
0
0

I need some help please.
In this code I create two objects (MM and FF) that extend an abstract
object (MF). Then I have a subroutine (execute_MM_or_FF) that is
supposed to handle both objects (MM and FF).
I tried several ways of doing it but none worked; this is an example
for which I get the error message
error #8169: The specified interface is not declared.   [OUTPUT]
What is the problem with this code? thanks

!===================================
module Abstract_class_m

    implicit none
    private

    type , abstract , public :: MF
    contains
        procedure (output_info) , deferred :: output
    end type

    abstract interface

        subroutine output_info( me , iter)
            import :: MF
            class(MF) , intent(in) :: me
            Integer   , intent(in) :: iter
        end subroutine

    end interface

end module Abstract_class_m
!===================================
module MM_class_m

    use Abstract_class_m 

    implicit none

    private

    public :: MM

    type,extends(MF):: MM
        integer                  :: ITMAX = 200        
        character (len=2)       :: driver
    contains
        procedure :: output => output_MM
    end type MM

    interface MM
        module procedure  constructor
    end interface

contains

function constructor( ) result( me )
implicit none

type(MM) :: me

me % driver = "MM"

end function constructor
!
!
subroutine output_MM( me , iter)
implicit none
class(MM)   , intent(in) :: me
integer     , intent(in) :: iter

print*, me%driver , iter

end subroutine output_MM

end module MM_class_m
!===================================
module FF_class_m

    use Abstract_class_m 

    implicit none

    private

    public :: FF

    type,extends(MF):: FF
        integer                  :: ITMAX = 20        
        character (len=2)       :: driver
    contains
        procedure :: output => output_FF
    end type FF

    interface FF
        module procedure  constructor
    end interface

contains

function constructor( ) result( me )
implicit none

type(FF) :: me

me % driver = "FF"

end function constructor
!
!
subroutine output_FF( me , iter)
implicit none
class(FF)   , intent(in) :: me
integer     , intent(in) :: iter

print*, me%driver , iter

end subroutine output_FF

end module FF_class_m
!===================================
module execute_MM_or_FF

        use MM_class_m          , only: MM
        use FF_class_m          , only: FF
        use Abstract_class_m    , only: MF

        implicit none

        public :: execute

        private

        type, extends(MF) :: OBJ
            integer          :: ITMAX
            character(len=2) :: driver
        contains
            procedure :: output
        end type

contains     

subroutine execute( me , n )
class(OBJ) , intent(inout) :: me
integer    , intent(in)    :: n

    print*, me%itmax
    call me%output(n)

end subroutine execute

end module execute_MM_or_FF
!===================================

program test

use MM_class_m              , only: MM
use FF_class_m                , only: FF
use execute_MM_or_FF   , only: execute

implicit none

character(2) :: driver = "MM"
integer      :: iter = 3

type(MM) :: a
type(FF) :: b

if(driver == "MM") then
    a = MM()
    call execute(a,iter)
else
    b = FF()
    call execute(b,iter)
end if

end program test


Viewing all articles
Browse latest Browse all 3270

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>