1

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

这两个类如下:

类 Type_Test:

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 = r  % get_r()  +  y % get_y()

Error: Operands of binary numeric operator '+' at (1) are TYPE(type_test_try)/REAL(4) in main_test

在 result_3 和 result_4 中。我知道在 type_test_try 中传递真实变量时有问题,但我真的不明白为什么会这样。

4

1 回答 1

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
于 2019-01-10T12:15:22.007 回答