现代Fortran吸气器/设置器

时间:2019-01-09 11:35:43

标签: oop fortran

我正在尝试一个包含两个类的代码,在一个包含一些基本操作的main之后。

这两个类如下:

Type_Test类:

init

课堂测试:

Module type_test

 implicit none

 public :: type_test_try

 Type type_test_try

   private

    logical :: h_set = .false.

      real    :: h = 7.0 

     contains

     private

      procedure, public :: get_h
      procedure, public :: set_h

 End Type type_test_try

  Contains

pure real function get_h( this ) result (h)
class(type_test_try), intent(in) :: this

   h = this % h

end function get_h

subroutine set_h( this, val )

 class(type_test_try), intent(inout) :: this
 real                , intent(in)    :: val

   this % h     =  val
   this % h_set = .true.

end subroutine set_h

End Module type_test

主程序类似于:

module test

use type_test, only: type_test_try

implicit none

  public :: test_1
  public :: test_2
  public :: test_3

  type test_1

   private

    logical ::  x_set  = .false. , &  
                y_set  = .false.


    real   :: x = 1.0
    real   :: y = 2.0

  contains

 private 

  procedure, public  :: get_x1 
  procedure, public  :: set_x1 

  procedure, public  :: get_y
  procedure, public  :: set_y      

end type test_1

type test_2

  private

  logical :: x_set = .false. , &
             z_set = .false.

  real :: x = 3.0
  real :: z = 4.0

  contains

   procedure, public  :: get_x
   procedure, public  :: set_x

   procedure, public  :: get_z
   procedure, public  :: set_z

end type test_2

type test_3

private

logical :: r_set = .false.

type(ref_type_test) :: r  

 contains

   procedure, public :: get_r
   procedure, public :: set_r

end type test_3

contains

real function get_x1(this) result ( x1 )

class(test_1), intent(in) :: this

    x1 = this % x

end function get_x1

subroutine set_x1( this, val )

class(test_1), intent(inout) :: this
real,          intent(in)    :: val

this % x     =  val 
this % x_set = .true.

end subroutine set_x1

real function get_y(this) result ( y )

 class(test_1), intent(in) :: this

     y = this % y

end function get_y

subroutine set_y( this, val )

 class(test_1), intent(inout) :: this
 real,          intent(in)    :: val

 this % y     =  val 
 this % y_set = .true.

end subroutine set_y

real function get_x( this ) result ( x )

class(test_2), intent(in) :: this

x = this % x

end function get_x

subroutine set_x( this, val )

class(test_2), intent(inout) :: this
real,          intent(in)    :: val

this % x     =  val 
this % x_set = .true.

end subroutine set_x

real function get_z( this ) result ( z )

 class(test_2), intent(in) :: this

     z = this % z

end function get_z

subroutine set_z( this, val )

 class(test_2), intent(inout) :: this
 real,          intent(in)    :: val

 this % z     =  val
 this % z_set = .true. 

end subroutine set_z

type(type_test_try) function get_r( this ) result( r )

 class(test_3), intent(in) :: this

    r = this % r

end function get_r

subroutine set_r( this, val )

 class(test_3) , intent(inout) :: this
 type(type_test_try), intent(in)    :: val

   this % r     =  val
   this % r_set = .true.

end subroutine set_r

end module test

编译时出现以下错误:

program main_test

 use test,      only: test_1
 use test,      only: test_2
 use test,      only: test_3
 use type_test, only: type_test_try

 implicit none

  real  :: result_1
  real  :: result_2
  real  :: result_3
  real  :: result_4

  type(test_1) :: x1 
  type(test_1) :: y  

  type(test_2) :: x 
  type(test_2) :: z

  type(test_3) :: r

  result_1 = x1 % get_x1() +  y % get_y()
  result_2 = x  % get_x()  +  z % get_z()

  result_3 = r  % get_r()  +  y % get_y()
  result_4 = r  % get_r()  +  z % get_z()

  print*,'1', result_1
  print*,'2', result_2
  print*,'3', result_3
  print*,'4', result_4

 end program main_test
对于result_3和result_4中的

。我知道在type_test_try中传递实变量时出了点问题,但是我真的不明白为什么会这样。

1 个答案:

答案 0 :(得分:3)

为了使默认运算符在定义的类型和内部类型之间起作用,您需要执行一些运算符重载。在Fortran中,这实际上非常简单明了。看:

Type_test

module type_test
  (...)
  interface operator(+)
    module procedure :: real_plus_test, test_plus_real
  end interface

contains
  pure real function real_plus_test(left, right)
    real, intent(in) :: left
    type(type_test_try), intent(in) :: right
    real_plus_test = left + right%h
  end
  pure real function test_plus_real(left, right)
    type(type_test_try), intent(in) :: left
    real, intent(in) :: right
    test_plus_real = left%h + right
  end
(...)
end module