final procedure problem

final procedure problem

here is my code

module vector_class
implicit none
! Type definition
type,public :: vector
   private 
   real, dimension(:), POINTER :: v
   logical :: v_allocated = .false.

contains
    procedure,public :: set_vector=>set_vector_sub
    procedure,public :: get_vector=>get_vector_sub
!    procedure,public :: clear_vector=>clear_vector_sub
   FINAL :: clear_vector 
end type vector

private :: set_vector_sub,get_vector_sub,clear_vector_sub

contains

subroutine set_vector_sub(this,array)
   implicit none
   class(vector) :: this
   real,dimension(:),intent(in) :: array
   integer istat
   if(this%v_allocated) then
      deallocate(this%v,stat=istat)
   endif

   allocate(this%v(size(array,1)),stat=istat)
   this%v = array
   this%v_allocated = .true.

end subroutine set_vector_sub

subroutine get_vector_sub(this, array)
   implicit none
   class(vector) :: this
   real,dimension(:),intent(out) :: array

   integer array_length
   integer data_length
   integer istat

   if(this%v_allocated) then
      array_length = size(array,1)
      data_length  = size(this%v,1)
      if(array_length > data_length) then
         array(1:data_length) = this%v
         array(data_length+1:array_length) = 0
      else if(array_length == data_length) then
         array = this%v
      else
         array = this%v(1:array_length)
      endif
   else
     array = 0
   endif
end subroutine get_vector_sub

subroutine clear_vector_sub(self)
   implicit none
   class(vector) :: self
   integer istat

   write(*,*) 'in finalizer .....'
   if(self%v_allocated) then
      deallocate(self%v,stat=istat)
   endif

end subroutine clear_vector_sub

end module vector_class

 

 

 program test_vector
 use vector_class
 implicit none
 type(vector) :: aa
 

 

 end program test_vector

following is compiler error :

vectorclass.f90(13): error #8338: A final subroutine name must be the name of a module procedure with exactly one dummy argument.   [CLEAR_VECTOR]
   FINAL :: clear_vector
------------^
compilation aborted for vectorclass.f90 (code 1)

i not kown why is IT error ?

 

6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

following is compiler error :

vectorclass.f90(13): error #8338: A final subroutine name must be the name of a module procedure with exactly one dummy argument.   [CLEAR_VECTOR]
   FINAL :: clear_vector
------------^
compilation aborted for vectorclass.f90 (code 1)

i not kown why is IT error ?

As the message says... the final subroutine name must be the name of a module procedure... and it isn't.

Perhaps you meant

FINAL :: clear_vector_sub
!                     ^^^

Note that entities of the main program are usually not finalised - if you want to see `aa` finalised, make it a local entity of a subroutine or similar.

The dummy argument of a final procedure also needs to be non-polymorphic.

my complier version ifort version 15.0.3

i modify my code :

   FINAL :: clear_vector

to 

   FINAL :: clear_vector_sub

but error is have

following is error message is :

vectorclass.f90(60): error #8339: The dummy argument of a final subroutine must be a variable of the derived type being defined and cannot be optional, pointer, allocatable, or polymorphic.   [THIS]
subroutine clear_vector_sub(this)
----------------------------^
compilation aborted for vectorclass.f90 (code 1)

following is  source code::

module vector_class
implicit none
! Type definition
type,public :: vector
   private 
   real, dimension(:), POINTER :: v
   logical :: v_allocated = .false.

contains
    procedure,public :: set_vector=>set_vector_sub
    procedure,public :: get_vector=>get_vector_sub
!    procedure,public :: clear_vector=>clear_vector_sub
   FINAL :: clear_vector_sub
end type vector

private :: set_vector_sub,get_vector_sub

contains

subroutine set_vector_sub(this,array)
   implicit none
   class(vector) :: this
   real,dimension(:),intent(in) :: array
   integer istat
   if(this%v_allocated) then
      deallocate(this%v,stat=istat)
   endif

   allocate(this%v(size(array,1)),stat=istat)
   this%v = array
   this%v_allocated = .true.

end subroutine set_vector_sub

subroutine get_vector_sub(this, array)
   implicit none
   class(vector) :: this
   real,dimension(:),intent(out) :: array

   integer array_length
   integer data_length
   integer istat

   if(this%v_allocated) then
      array_length = size(array,1)
      data_length  = size(this%v,1)
      if(array_length > data_length) then
         array(1:data_length) = this%v
         array(data_length+1:array_length) = 0
      else if(array_length == data_length) then
         array = this%v
      else
         array = this%v(1:array_length)
      endif
   else
     array = 0
   endif
end subroutine get_vector_sub

subroutine clear_vector_sub(this)
   implicit none
   class(vector) :: this
   integer istat

   write(*,*) 'in finalizer .....'
   if(this%v_allocated) then
      deallocate(this%v,stat=istat)
   endif

end subroutine clear_vector_sub

end module vector_class

 

 

 program test_vector
 use vector_class
 implicit none
 type(vector) :: aa
 

 

 end program test_vector

 

 

See the last sentence in reply #3.  The code still declares the dummy argument as polymorphic - using CLASS(vector).  It needs to be non-polymorphic - TYPE(vector)
 

thank you! it‘s ok。

Leave a Comment

Please sign in to add a comment. Not a member? Join today