인수로 유형 바인딩된 프로시저
-
20-12-2019 - |
문제
다음과 같이 유형 바인딩 프로시저(외부 함수)를 다른 함수에 전달하고 싶습니다.
module mod1
implicit none
type type1
real :: a
contains
procedure,pass :: f
end type
contains
real function f(y,e)
class(type1), intent(in) :: y
real,intent(in) :: e
f=y%a+e
end function
end module
program test
use mod1
type(type1) :: t
t%a=3e0
write(*,*) s(t%f)
contains
real function s(g)
real,external :: g
s=g(5e0)+2e0
end function
end program
gfortran이 생성하면 다음 오류가 발생합니다.
write(*,*) s(t%f)
1
Error: Expected argument list at (1)
하지만 내가 할 수 있는 일은 다음과 같습니다.
program test
t%a=3e0
write(*,*) s(k)
contains
real function s(g)
real,external :: g
s=g(5e0)+2e0
end function
real function k(e)
real,intent(in) :: e
k=3e0+e
end function
end program
내 생각엔 문제가 다음과 관련이 있는 것 같아 유형 바인딩 프로시저를 인수로 전달, 하지만 현재로서는 거기에 있는 답변이 나에게 어떻게 도움이 될 수 있는지 알 수 없습니다.
편집하다:
(희망적으로) 어려움을 보여주는 더 나은 예:
module mod2
implicit none
contains
real function s(g)
interface
real function g(x)
real, intent(in) :: x
end function
end interface
s=g(5e0)+2e0
end function
end module
module mod1
use mod2
type type1
real :: a
contains
procedure,pass :: f
procedure,pass :: h
end type
contains
real function f(y,e)
class(type1), intent(in) :: y
real,intent(in) :: e
f=y%a+e
end function
real function h(y)
class(type1), intent(inout) :: y
h=s(y%f)
end function
end module
program test
use mod1
type(type1) :: t
t%a=3e0
write(*,*) t%h
end program
편집 II:좋습니다. 래퍼는 여전히 포인터와 함께 작동합니다.
module mod2
implicit none
contains
real function s(g)
interface
real function g(x)
real, intent(in) :: x
end function
end interface
s=g(5e0)+2e0
end function
end module
module mod1
use mod2
type type1
real :: a
contains
procedure,pass :: f
procedure,pass :: h
end type
class(type1),pointer :: help_w
contains
real function f(y,e)
class(type1), intent(in) :: y
real,intent(in) :: e
f=y%a+e
end function
real function h(y)
class(type1), intent(inout),target :: y
help_w => y
h=s(wrap)
end function
function wrap(x)
real,intent(in) :: x
wrap=help_w%f(x)
end function
end module
program test
use mod1
type(type1) :: t
t%a=3e0
write(*,*) t%h()
end program
이것은 확실히 아름다운 해결책은 아니지만 적어도 작동합니다.
해결책
래퍼를 작성할 수 있습니다.이것은 가장 간단한 버전입니다.내부 함수를 더미 인수(F2008)로 전달해야 하지만, 다음과 같은 경우 모듈에서 래퍼를 선언할 수도 있습니다. t
거기 있을 수 있어.
참고 프로시저 인수 선언을 변경했습니다. s
좀 더 현대적인 것, 즉 인터페이스 블록으로.
program test
use mod1
type(type1) :: t
t%a=3e0
write(*,*) s(wrap)
contains
real function s(g)
interface
real function g(x)
real, intent(in) :: x
end function
end interface
s=g(5e0)+2e0
end function
function wrap(x)
real, intent(in) :: x
wrap = t%f(x)
end function
end program
오류의 원인은 연결된 질문에 대한 답변에 잘 설명되어 있습니다. 시도한 방식으로 유형 바인딩 프로시저를 전달할 수 없습니다.
제휴하지 않습니다 StackOverflow