2012-01-05 87 views
3

我試圖從fortran函數返回一個類型。這是代碼。爲什麼不能訪問該類型?

module somemodule 
implicit none 
! define a simple type 
type sometype 
    integer :: someint 
end type sometype 
! define an interface 
interface 
    ! define a function that returns the previously defined type 
    type(sometype) function somefunction() 
    end function somefunction 
end interface 
contains 
end module somemodule 

在gfortran(4.4 & 4.5)我收到以下錯誤:

Error: The type for function 'somefunction' at (1) is not accessible

我編譯的文件:

gfortran -c ./test.F90 

我試圖讓類型明確公開,但該沒有幫助。我打算使用某種功能的c版本,這就是爲什麼我把它放在界面部分。

爲什麼類型不可訪問?

回答

5

加入導入函數定義裏面定義了這個。由於許多人認爲語言設計中存在錯誤,因此定義不會在接口中繼承。 「導入」優先於此來實現明智的行爲。

interface 
    ! define a function that returns the previously defined type 
    type(sometype) function somefunction() 
    import 
    end function somefunction 
end interface 
3

問題爲何無法訪問的答案是標準委員會就是這樣設計的。該接口與封閉模塊有一個單獨的作用域,因此您必須從中明確導入名稱。顯然(?)你不能在use模塊內部,所以需要import聲明。

相關問題