2013-05-01 70 views
-1

我正在使用派生類型編寫Fortran中的一些代碼並遇到問題,但仍無法弄清楚發生了什麼問題................... .................................................. .................................................. .................................................. .................................................. .................................................. .............使用派生類型時的Fortran錯誤

make -f vbld.mk 
gfortran -c gshapes.f08 
gshapes.f08:100.31: 

     generic, public :: get => get_ellipse,  & 
           1 
Error: Undefined specific binding 'get_ellipse_minmax' as target of GENERIC 'get' at (1) 
gshapes.f08:136.31: 

     generic, public :: get => get_cylinder,  & 
           1 
Error: Undefined specific binding 'get_cylinder_minmax' as target of GENERIC 'get' at (1) 
gshapes.f08:139.15: 

     procedure :: print => print_cylinder 
       1 
Error: Dummy argument 'cyld' of 'print' at (1) should be named 'elips' as to match the  
corresponding argument of the overridden procedure 
gshapes.f08:135.15: 

     procedure :: set => set_cylinder 
       1 
Error: Dummy argument 'cyld' of 'set' at (1) should be named 'elips' as to match the 
corresponding argument of the overridden procedure 
gshapes.f08:74.31: 

     generic, public :: get => get_rectangle,  & 
           1 
Error: Undefined specific binding 'get_rectangle_minmax' as target of GENERIC 'get' at (1) 
gshapes.f08:118.31: 

     generic, public :: get => get_prism,  & 
           1 

Error: Undefined specific binding 'get_prism_minmax' as target of GENERIC 'get' at (1) 
gshapes.f08:121.15: 

     procedure :: print => print_prism 
       1 
Error: Dummy argument 'prsm' of 'print' at (1) should be named 'rect' as to match the 
corresponding argument of the overridden procedure 
gshapes.f08:117.15: 

     procedure :: set => set_prism 
       1 
Error: Dummy argument 'prsm' of 'set' at (1) should be named 'rect' as to match the 
corresponding argument of the overridden procedure 
make: *** [gshapes.mod] Error 1 
+0

顯示相關代碼。錯誤表明在類型綁定的泛型語句中有過程名稱,而不是特定的綁定名稱;以及特定結合覆蓋中的一些參數特徵不匹配。 – IanH 2013-05-01 02:00:26

回答

0
module shape 
implicit none 

type, public :: GCoord 
    real :: x 
    real :: y 
    real :: z 
end type GCoord 

type, extends (GCoord) :: GCentr 
end type GCentr 

type, extends (GCoord) :: GCornr 
end type GCornr 

type, public :: Rectangle 
    type(GCentr) :: cn 
    type(GCoord) :: pa 
    real :: b 
    type(GCornr) :: p1, p2, p3, p4 
    contains 
    generic, public :: get => getu, getm 
end type Rectangle 

contains 

subroutine getu (rect, val) 
    class(Rectangle), intent(in) :: rect 
    integer, intent(in) :: val 
    write (*,*) 'GETU' 
end subroutine getu 

subroutine getm (rect, val) 
    class(Rectangle), intent(in) :: rect 
    complex, intent(in) :: val 
    write (*,*) 'GETM' 
end subroutine getm 

end module shape 
+0

我做了上面的例子,我得到的錯誤是:rectangle.f08:52.31: generic,public :: get => getu,getm 錯誤:未定義特定綁定'getm'作爲GENERIC'get'的目標(1) – Pingu 2013-05-01 20:46:16

+0

編輯您的問題並將示例代碼放在那裏。 – IanH 2013-05-01 21:54:50

2

一個通用的結合提名特定綁定當通用綁定是和借鑑,可以考慮編碼。然後這些特定的綁定引用特定的過程(這些特定的過程可以被覆蓋 - 它們可能是擴展中的不同特定過程)。

您的通用綁定引用過程名稱而不是特定綁定名稱。正確的做法是:

TYPE parent 
CONTAINS 
    PROCEDURE :: SpecificBindingA => ProcedureA 
    PROCEDURE :: SpecificBindingB => ProcedureB 
    GENERIC :: GenericBinding => SpecificBindingA, SpecificBindingB 
END TYPE parent 

在代碼中,如果有聲明TYPE(parent) :: obj一個對象,然後obj%GenericBinding參考要麼解析爲obj%SpecificBindingAobj%SpecificBindingB取決於在參考的實際參數的類型。然後obj的動態類型將確定爲特定特定綁定調用的實際過程。

過程ProcedureA和ProcedureB將需要聲明其第一個僞參數爲適當的,以便它可以是傳入的對象(它必須聲明爲CLASS(parent),具有相同的參數名稱等)。父項擴展中的任何覆蓋都需要適當地改變所傳遞參數的類型,並且具有任何其他僞參數(包括僞參數名稱)匹配的特徵。

另外,也許你只是想要一個具體程序的通用名稱。你使用接口塊來做到這一點。

INTERFACE GenericName 
    MODULE PROCEDURE ProcedureA 
    MODULE PROCEDURE ProcedureB 
END INTERFACE GenericName 

在這種情況下,參考GenericName(...)將根據在參考的參數被解析爲ProcedureAProcedureB。在這種情況下,不存在基於對象的動態類型的特定過程的動態查找。