Modern Fortran getter/setter编译报错:类型不匹配问题咨询
Hey there, let's break down why you're hitting that error and how to fix it!
The Root Cause
The error message spells it out clearly: you’re trying to add a type_test_try object (returned by r%get_r()) and a real number (from y%get_y()), but Fortran has no built-in rule for this operation. Your get_r() function returns the entire type_test_try instance, not the real value h stored inside it—so the compiler can’t resolve the + operator between those two different types.
Fix Options
Here are three straightforward ways to resolve this:
Solution 1: Modify test_3 to return the real value directly
Change the get_r function in the test module to fetch and return the h value from the nested type_test_try object instead of returning the whole object:
! Update the get_r function in the test module real function get_r( this ) result( r_val ) class(test_3), intent(in) :: this r_val = this % r % get_h() end function get_r
Then update the procedure declaration in test_3 to match the new return type:
type test_3 private logical :: r_set = .false. type(type_test_try) :: r contains procedure, public :: get_r ! Now returns real instead of type_test_try procedure, public :: set_r end type test_3
Now r%get_r() will return a real number that can safely be added to y%get_y().
Solution 2: Extract the real value in the main program
If you don’t want to modify the test_3 module, adjust the main program to explicitly call get_h() on the returned type_test_try object:
! Fix the result lines in main_test result_3 = r % get_r() % get_h() + y % get_y() result_4 = r % get_r() % get_h() + z % get_z()
This pulls out the real h value before performing the addition.
Solution 3: Overload the + operator for type_test_try
If you want type_test_try objects to be directly addable to real numbers, define an operator overload in the type_test module:
Module type_test implicit none public :: type_test_try public :: operator(+) ! Declare the overloaded operator as public Type type_test_try private logical :: h_set = .false. real :: h = 7.0 contains private procedure, public :: get_h procedure, public :: set_h procedure, private :: add_type_real ! Handles type + real generic, public :: operator(+) => add_type_real ! Map + to the procedure End Type type_test_try Contains ! ... existing get_h and set_h functions ... ! Define addition between type_test_try and real pure real function add_type_real( this, val ) result(res) class(type_test_try), intent(in) :: this real, intent(in) :: val res = this%h + val end function add_type_real ! Optional: Add support for real + type_test_try too pure real function add_real_type( val, this ) result(res) real, intent(in) :: val class(type_test_try), intent(in) :: this res = val + this%h end function add_real_type ! Uncomment below to include this in the operator overload ! generic, public :: operator(+) => add_type_real, add_real_type End Module type_test
With this overload, your original main program code will work as-is—Fortran now knows how to handle adding a type_test_try object to a real number.
内容的提问来源于stack exchange,提问作者Jack MR

