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

error # 8227 type - dynamic type pointer mismatch - Fortran V12.0

Terry_Gerald1
Beginner
1,207 Views
Encountered an error wihen using a "derived" type.

I searched the web for discussions cocerning this error.


Found an example that reproduces the same error with Intel Fortran 12.0 I am getting with my code -
http://fortranwiki.org/fortran/show/Factory+Pattern





module connection_mod

type CFactory
private
character(len=20) :: factory_type
class(Connection), pointer :: connection_type
contains
procedure :: init
procedure :: create_connection
end type CFactory


type, abstract :: Connection
contains
procedure(generic_desc), deferred, pass(self) :: description
end type Connection


abstract interface
subroutine generic_desc(self)
import :: Connection
class(Connection), intent(in) :: self
end subroutine generic_desc
end interface

!! An Oracle connection

type, extends(Connection) :: OracleConnection
contains
procedure, pass(self) :: description => oracle_desc
end type OracleConnection

!! A MySQL connection

type, extends(Connection) :: MySQLConnection
contains
procedure, pass(self) :: description => mysql_desc
end type MySQLConnection


contains

subroutine init(self, string)
class(CFactory), intent(inout) :: self
character(len=*), intent(in) :: string
self%factory_type = trim(string)
self%connection_type => null() !! pointer is nullified
end subroutine init


function create_connection(self) result(ptr)

class(CFactory) :: self
class(Connection), pointer :: ptr

if(self%factory_type == "Oracle") then

if(associated(self%connection_type)) deallocate(self%connection_type)
allocate(OracleConnection :: self%connection_type)
ptr => self%connection_type

elseif(self%factory_type == "MySQL") then

if(associated(self%connection_type)) deallocate(self%connection_type)
allocate(MySQLConnection :: self%connection_type)
ptr => self%connection_type

end if
end function create_connection

subroutine oracle_desc(self)
class(OracleConnection), intent(in) :: self
write(*,'(A)') "You are now connected with Oracle"
end subroutine oracle_desc

subroutine mysql_desc(self)
class(MySQLConnection), intent(in) :: self
write(*,'(A)') "You are now connected with MySQL"
end subroutine mysql_desc


end module connection_mod


program poly

use connection_mod

implicit none

type(CFactory) :: factory
class(Connection), pointer :: db_connect => null()

call factory%init("Oracle")
db_connect => factory%create_connection() !! Create Oracle DB
call db_connect%description()

!! The same factory can be used to create different connections
call factory%init("MySQL") !! Create MySQL DB

!! 'connect' is a 'class' pointer. So can be used for either Oracle or MySQL
db_connect => factory%create_connection()
call db_connect%description()

call factory%final() ! Destroy the object

write(*,*) 'Finished'

end




0 Kudos
5 Replies
Steven_L_Intel1
Employee
1,207 Views
This example shows more errors. How about you show us an example of your code that gets just the particular error? In the program you posted, the compiler complains earlier. I'll have to study this to see if the errors are warranted.
0 Kudos
Terry_Gerald1
Beginner
1,207 Views
Steve,


My code is significantly longer - thats why I posted the above code. The error appears to be the same.


The pertinent error isin function"create_connection"


ptr => self%connection_type

where we declared :class(Connection), pointer :: ptr



I have a similar construcrt in my code where I attempt to set a "base class" pointer to a "derived class" object.

I get :

error #8227 "The type of the target is not a dynamic type of the pointer [PTR]"



If you want my actual code - I guess I will have to edit it down.



0 Kudos
Steven_L_Intel1
Employee
1,207 Views
You should be able to edit it down to something small.
0 Kudos
Terry_Gerald1
Beginner
1,207 Views

Code snippet follows:


module mesh_mod


type :: node_type
real :: x,y,z
end type node_type


type :: mesh_type
type(node_type), allocatable :: nodes
class(integrator_type), pointer :: integrator
contains
procedure integrate => mesh_type_integrate
end type mesh_type


type, abstract :: integrator_type
contains
procedure(interface_integrate), deferred, pass(this) :: integrate
end type integrator_type


abstract interface
subroutine interface_integrate(this, mesh, time)
import :: integrator_type, mesh_type
class(integrator_type) :: this
type(mesh_type), pointer :: mesh
real :: time
end subroutine interface_integrate
end interface


type, extends(integrator_type) :: CGM_type
contains
procedure :: integrate => CGM_type_integrate
end type CGM_type


type, extends(integrator_type) :: DGM_type
contains
procedure :: integrate => DGM_type_integrate
end type DGM_type


contains


function create_mesh(solver) result(mesh)
character(len=*) :: solver
type(mesh_type), pointer :: mesh

type(CGM_type), pointer :: cgm
type(DGM_type), pointer :: dgm
class(integrator_type), pointer :: integrator

allocate(mesh)

select case(solver)
case('CGM','cgm')
! NOTE A: First attempt:
!allocate(CGM_type::mesh%integrator)

! NOTE B: Second attempt
!allocate(cgm)
!mesh%integrator => cgm

! NOTE C: Third attempt
integrator => integrator_create('CGM')
mesh%integrator => integrator

case('DGM','dgm')
! NOTE A:First attempt
!allocate(DGM_type::mesh%integrator)

! NOTE B:Second attempt
!allocate(dgm)
!mesh%integrator => dgm

! NOTE C:Third attempt
integrator => integrator_create('DGM')
mesh%integrator => integrator

case default
nullify(mesh%integrator)
end select

end function create_mesh


subroutine CGM_type_integrate(this, mesh, time)
class(CGM_type) :: this
type(mesh_type), pointer :: mesh
real :: time
! Do mesh node mechanics
end subroutine CGM_type_integrate


subroutine DGM_type_integrate(this, mesh, time)
class(DGM_type) :: this
type(mesh_type), pointer :: mesh
real :: time
! Do mesh node mechanics
end subroutine DGM_type_integrate


subroutine mesh_type_integrate(this, time)
class(mesh_type) :: this
real :: time
class(integrator_type), pointer :: integrator


! NOTE D: Should work - compiler complains
!integrator => this%integrator
!call integrator%integrate(this, time)

call this%integrator%integrate(this, time)

end subroutine mesh_type_integrate

function integrator_create(integrator_name) result(integrator)
character(len=*) :: integrator_name
class(integrator_type), pointer :: integrator
type(CGM_type), pointer :: cgm
type(DGM_type), pointer :: dgm

nullify(integrator)
select case(integrator_name)
case('CGM','cgm')
allocate(cgm)
integrator => cgm
case('DGM','dgm')
allocate(dgm)
integrator => dgm
case default
nullify(integrator)
end select

end function integrator_create

end module mesh_mod


program poly

use mesh_mod

type(mesh_type), pointer :: mesh => null()
real :: time = 1.0

mesh => mesh_create('CGM')

call mesh%integrate(time)

end



I labelledproblemconstructs with "NOTE A, NOTE B, NOTE C and NOTE D"

0 Kudos
Steven_L_Intel1
Employee
1,207 Views
Thanks - I'll take a look at this.
0 Kudos
Reply