2012-07-03 5 views
3

다른 유형의 서브 루틴에 인수로 유형 바인드 프로 시저를 전달하려고합니다. 이것이 Fortran에서 가능한지 알고 싶습니다. 다음은 내가하려고하는 것을 보여주는 코드 스 니펫입니다.인수로 유형 바인드 프로 시저 전달

module type_definitions 
type test_type 
integer :: i1, i2,i3 
contains 
    procedure :: add_integers_up 
end type test_type 
contains 
    subroutine add_integers_up(this,i4,ans) 
     class(test_type) :: this 
     integer :: i4,ans 
     ans = this%i1+this%i2+this%i3+i4 
    end subroutine add_integers_up 

subroutine print_result_of_subroutine(i4,random_subroutine) 
    integer :: i4,ans 

    interface 
    subroutine random_subroutine(i1,i2) 
     integer:: i1,i2 
    end subroutine random_subroutine 
    end interface 

    call random_subroutine(i4,ans) 
    write(*,*) ans 


end subroutine print_result_of_subroutine 


end module type_definitions 


program main 
    use type_definitions 
    implicit none 
    integer :: i1,i2,i3,i4 
    integer :: ans 
    type(test_type) :: test_obj 

    i1 =1; i2=2; i3=3 
    test_obj%i1 = i1 
    test_obj%i2 = i2 
    test_obj%i3 = i3 
    i4 = 4 

    call print_result_of_subroutine(i4,test_obj%add_integers_up) 

    end program main 

이것은 포트란에서 가능합니까? ifort를 사용하여이 코드를 컴파일하려고하면 컴파일러 오류가 발생합니다.

답변

5

test_obj % add_integers_up은 프로 시저가 아닙니다. add_integers_up이라는 프로 시저에 바인딩되는 프로 시저입니다. 바인딩을 실제 인수로 전달할 수 없습니다.

바인딩이 연결된 특정 프로 시저를 전달하려면 프로 시저를 전달하십시오! 가설 :

call print_result_of_subroutine(i4, add_integers_up) 

그러나 다른 포스터가 언급 한 것처럼, 당신의 예제 코드에서 해당 프로 시저의 인터페이스가 print_result_of_subroutine에 해당하는 더미 인수의 인터페이스와 일치하지 않습니다.

test_obj % add_integers_up이 연관된 프로 시저 포인터 구성 요소를 참조하고 (해당 구성 요소의 인터페이스가 print_result_of_subroutine에 의해 예상되는 것과 일치하는 경우) 예상대로 나타나면 작동합니다.

포트란 90 형 바인딩 절차 (또는 프로 시저 포인터 구성 요소)를 지원하지 않는

주 - 당신의 코드는 매우 정확하게 포트란 2003

에게
3

정확한 오류 메시지가 표시되지 않았고 직접 예제를 시도하지 않았지만 프로 시저 임시 인수의 인터페이스가 해당 프로 시저 임시 인수에 해당하지 않는다는 것이 확실합니다. 전달 된 실제 인수의 인터페이스.

더욱 명시 적으로 random_subroutine은 두 개의 인수를 취하는 것으로 선언되며 test_obj%add_integers_up은 세 개의 인수를 취합니다. 그것들 중 하나가 전달 된 객체의 더미 인수로서 기능하더라도, 여전히 그 프로 시저의 인터페이스의 일부로 간주됩니다.

+0

이 필요합니다! 인터페이스 블록에서 '절차'또는 '모듈 프로 시저'를 사용하고 직접 인터페이스를 지정하지 않도록 추가 할 수 있습니다. –