考虑
INTEGER,DIMENSION(3) :: NumberVector
Run Code Online (Sandbox Code Playgroud)
和
INTEGER :: NumberVector(3)
Run Code Online (Sandbox Code Playgroud)
这两个声明之间是否有任何差异,或者它们是否完全相同?(我的意思是在任何可能的上下文和变体中:例如,在这两个是相同的情况下,如果我声明一个隐式大小的数组作为子程序的输入参数之一怎么办?它仍然是无关紧要的我用了?)
请参阅下面的示例
program test
character(10),dimension(5):: models = (/"feddes.swp", "jarvis89.swp", "jarvis10.swp" , "pem.swp", "van.swp"/)
end
Run Code Online (Sandbox Code Playgroud)
返回以下错误:
数组构造函数中不同的CHARACTER长度(10/12)(1)
ifort编译器没有错误.为什么gfortran会发生这种情况,有没有办法解决这个问题?
我想通过class(*)功能(无限多态性)实现有用的数组操作(添加元素、删除元素、可分配/指针/二叉树结构的不同实现)。我使用 gfortran 5.0 应该可以处理这样的功能。我需要它,以免为我使用的每种类型重复相同的代码。
这应该看起来像
function add_element(array,element)
class(*),intent(in)::array(:)
class(*),intent(in)::element
class(*)::add_element(size(array)+1)
add_element=[array,element]
end function
Run Code Online (Sandbox Code Playgroud)
问题是,当我尝试将此函数与某些确定类型一起使用时,返回结果时出现错误。如果没有,我无法分配class(*)给某些明确的类型变量select type,并且我当然不希望每次使用它时都选择类型结构。在子例程中,我不应该知道我想要使用的任何类型,因为我将创建其中的许多类型。
我尝试了一些变体move_alloc,源,尝试使用带参数的子例程intent(out)等。它不起作用。我认为它应该在参数属性中定义,与大小相同(带有源关键字?),但在标准中没有找到此类结构的示例或定义。当然,我会更多地研究这个标准(我不是专业程序员,而是物理学家,试图使我的程序可测试、可检查并且更容易更改),并且现在将简单地重复此代码以等待更好的解决方案,但也许有人知道在哪里在标准或某本书中搜索它?我认为这不仅与数组有关,而且与数组的使用有关class(*),因为我认为应该有一些不知道类型的方法......
不知道我是否应该添加该子例程的其他无效形式的示例或它对错误的说明 - 否则问题将失去焦点。它可以被编译,但在所有情况下,在调用中分配给明确的类型都不起作用。对于论证intent(out)来说,(inout)它不能从虚拟论证变成实际论证。从源重新分配会生成一个具有类型的对象(以及在我的示例中分配的结果),但该类型是隐藏的......并且我无法在子例程中使用选择类型,因为我不知道该类型。
另外,我不知道可以检查“相同类型”或在这种情况下的某些内容的构造......
我经常看到人们使用该OPEN语句而不明确指定STATUS. 在 Fortran 90 和 2008 标准中,这样说STATUS:
如果指定 UNKNOWN,则状态取决于处理器。如果省略此说明符,则默认值为 UNKNOWN。
我将此解释为,如果STATUS省略,任何事情都可能发生,具体取决于您使用的机器。
然而,从进行一些测试来看,默认行为(当STATUS省略时)似乎是REPLACE. 但我找不到 gfortran 编译器手册(来自https://gcc.gnu.org/onlinedocs/)中记录的这种行为。
问题:这REPLACE确实是 gfortran 和 ifort 等流行编译器的默认行为吗?如果是这样,这实际上有记录吗(但我只是碰巧没有找到它)?
我正在使用几种派生类型.出于调试目的,我想通过使用一个很好的方式在屏幕上打印它们generic :: write(formatted).
这是一个显示我的意思的示例程序:
program main
use :: test_mod
implicit none
type(test1) :: t1
type(test2) :: t2
t1 = test1(name="t1")
t2 = test2(name="t2", t1)
print *, 'Test1:', t1
print *, 'Test2:', t2
end program
Run Code Online (Sandbox Code Playgroud)
这是模块:
module test_mod
implicit none
private
public :: test1, test2
type :: test1
character(2) :: name
contains
procedure, private :: test1_writef
generic :: write(formatted) => test1_writef
end type test1
type :: test2
character(2) :: name
type(test1) :: t1
contains
procedure, private :: test2_writef
generic …Run Code Online (Sandbox Code Playgroud) 我想将C函数与Fortran中的相应结构相链接
struct ovf_file {
bool found;
bool is_ovf;
int n_segments;
struct ovf_file_handle *_file_handle;
};
DLLEXPORT struct ovf_file * ovf_open(const char *filename);
Run Code Online (Sandbox Code Playgroud)
这是我尝试这样做的:
module ovf
use, intrinsic :: iso_c_binding
implicit none
type, bind(c) :: ovf_file
logical(c_bool) :: found
logical(c_bool) :: is_ovf
integer(c_int) :: n_segments
type(c_ptr) :: file_handle
end type ovf_file
end module ovf
program main
use ovf
use, intrinsic :: iso_c_binding
implicit none
type(ovf_file) :: file_handle
interface
function ovf_open(filename) bind ( C, name = "ovf_open" ) result(handle)
character(len=1, kind=c_char), intent(in) :: …Run Code Online (Sandbox Code Playgroud) program main
real, parameter :: a = 1
!real :: a
!a=1
res = func(a)
write(*,*) res
end program main
function func(a)
real, parameter :: b=a+1 !(*)
func = b
return
end function func
Run Code Online (Sandbox Code Playgroud)
我的编译器在标记为(*)的行处抱怨.有没有办法用一个超出该函数的值来设置常量的值?
在Fortran 2003/8中使用类,尤其是扩展类型时,是否有任何模拟python的super()函数可用于从扩展类型中调用的扩展类型中调用方法?
我有一些需要组合的Fortran和C代码.
我正在使用看起来像这样的Fortran界面:
module bridge
use, intrinsic::iso_c_binding, only : c_ptr, c_null_ptr
implicit none
type(c_ptr) :: instance
interface
function c_init() result(this) bind(C, name="bridge_init")
import
type(c_ptr) :: this
end function c_init
end interface
contains
subroutine init()
instance = c_init()
end subroutine init
end module bridge
Run Code Online (Sandbox Code Playgroud)
我的问题是我想在init子程序中检查初始化,例如
subroutine init()
if( instance .eq. c_null_ptr ) then
instance = c_init()
end if
end subroutine
Run Code Online (Sandbox Code Playgroud)
但这给了我一个Syntax error, found END-OF-STATEMENT when expecting one of: BLOCK BLOCKDATA PROGRAM MODULE TYPE INTEGER REAL COMPLEX BYTE CHARACTER CLASS …
我想在我的Fortran代码中为派生类型实现用户定义的I/O过程.但是,write这些过程中的语句不能在两个连续write语句之间产生新行.派生类型和过程定义如下.
模块:
module station_module
implicit none
character(8), parameter :: FmtFloat = '(5E15.7)'
type :: station
integer, private :: ns = 0
real, public, allocatable :: xloc(:), yloc(:), zloc(:)
contains
procedure, public :: import_station
procedure, public :: export_station
procedure, private :: read_station
generic, public :: read (formatted) => read_station
procedure, private :: write_station
generic, public :: write (formatted) => write_station
final :: destruct_station
end type station
interface station
module procedure new_station
end interface station
contains
function new_station(n) …Run Code Online (Sandbox Code Playgroud)