关于过程指针作为类成员的问题

时间:2019-01-17 17:52:16

标签: pointers fortran procedure

我写了3个文件

文件m_father.f90(父类):

module m_father
   implicit none
   type :: t_father
     real :: x(3)
     procedure(add_),nopass,pointer :: add_nopass=>add
   end type t_father

   abstract interface
     function add_(this) result(out)
        import t_father
        implicit none
        class(t_father),intent(in) :: this
        real :: out
     end function add_
   end interface
   private add_,add
contains
   function add(this) result(out)
      implicit none
      class(t_father),intent(in) :: this
      real :: out
      out=sum(this%x)
      end function add
   end module m_father

文件m_son.f90(son类):

module m_son
  use m_father
  implicit none
  type,extends(t_father) :: t_son
     real :: y(3)
  end type t_son
  private add
contains
  function add(this) result(out)
    implicit none
    class(t_son),intent(in) :: this
    real :: out
    out=sum(this%x)+sum(this%y)
  end function add
end module

文件main.f90

program main
   use m_son
   type(t_father) :: temp

   temp%x=[1,2,3]
   call temp%add_nopass(temp)
end program main

当我使用

gfortran -c m_father.f90 m_son.f90 

没关系。但是,当我编译main.f90时,这是错误的。

gfortran -c main.f90

这样的错误:

main.f90:2:6:    use m_son
      1 Error: Interface mismatch in procedure pointer assignment at (1): Type mismatch in argument 'this' (CLASS(t_father)/CLASS(t_son))

...

   use m_son
      1 Error: Interface mismatch for procedure-pointer component ‘add_nopass’ in structure constructor at (1): Type mismatch in argument 'this' (CLASS(t_father)/CLASS(t_son)) main.f90:8:11:

   call temp%add_nopass(temp)
           1 Error: FUNCTION attribute conflicts with SUBROUTINE attribute in ‘add_nopass’ at (1)

为什么错了?

为什么t_father中的过程指针add_nopass与文件m_son.f90中的私有函数add()冲突?

如何更正它,但仍然使用过程指针,并且不要更改文件m_son.f90中的函数名称add()。

0 个答案:

没有答案