我试图更好地了解指针如何在fortran上工作以及如何可以用作其他函数的参数。该程序的工作方式如下:它具有三个条目r0,rf
和long
。然后,函数linspace
生成一个从r0
到rf
的等距数组。然后,第二个函数应将半径值作为输入并取每个值的平方并生成一个新数组。这是代码
module class_Circle
use, intrinsic :: iso_fortran_env, only: dp => real64
implicit none
type, public :: Circle(long)
integer,len :: long
real(dp) :: r0,rf
contains
procedure :: radius =>linspace
procedure :: area => circle_area
end type Circle
contains
function linspace(this) result(radius)
class(Circle(*)),intent(inout) :: this
real(dp),dimension(this%long) :: radius
integer :: i
real(dp) :: incremento,b
incremento = (this%rf-this%r0)/(1.0d0*(this%long-1))
b = (this%r0*this%long-this%rf)/(1.0d0*(this%long-1))
do i = 1,this%long
radius(i) = b+incremento*i
end do
end function linspace
function circle_area (this) result(area)
class(Circle(*)), intent(inout) :: this
real (dp), dimension(this%long):: area
integer :: i
real(dp), parameter :: pi =dacos(-1.0d0)
do i = 1,this%long
area(i) = pi * this%radius(i)**2
end do
end function circle_area
end module class_Circle
program circle_test
use, intrinsic :: iso_fortran_env, only: dp => real64
use class_Circle
implicit none
integer,parameter :: long = 3
type(Circle(long)) :: c
c%r0 = 1.5d0
c%rf = 3.0d0
print*, c%radius()
print*, c%area()
end program circle_test
编译gfortran-8
时出现以下错误
area(i) = pi * this%radius(i)**2.0d0
1
Error: ‘radius’ at (1) is not a member of the ‘Pdtcircle’ structure
objecttest03.f90:10:15:
procedure :: area => circle_area
1
Error: Argument ‘this’ of ‘circle_area’ with PASS(this) at (1) must be of
the derived-type ‘circle' objecttest03.f90:9:15:
procedure :: radius =>linspace
1
Error: Argument ‘this’ of ‘linspace’ with PASS(this) at (1) must be of the derived-type ‘circle’
我不明白第一个错误,因为它是在类中定义的。另外,我想以下错误是由于这一原因造成的,那么我该如何进行这项工作?还有一个相关的问题,我可以指向不同模块中的过程吗?