- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page