Вопрос по поводу указателя на процедуру как члена класса - PullRequest
0 голосов
/ 17 января 2019

Я написал 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 (класс сына):

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)

Почему это не так?

Почему указатель процедуры add_nopass в t_father конфликтует с закрытой функцией add () в файле m_son.f90?

как исправить это, но все еще использует указатель процедуры и не изменять имя функции add () в файле m_son.f90.

...