创建数组时在 Fortran 中出现意外的数据声明错误 [英] Unexpected data declaration error in Fortran when creating array

查看:39
本文介绍了创建数组时在 Fortran 中出现意外的数据声明错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的测试程序来演示我在编译一些 Fortran 代码时收到的数据声明错误.编译错误发生在我试图创建任意大小的数组的行上.在 C 代码中,我相信这可以通过简单的 malloc 来完成,但这种方法在 Fortran 中可能没有用处.

I've written a simple test program to demonstrate a data declaration error that I am receiving when compiling some Fortran code. The compile error occurs on a line where I am trying to create an array of arbitrary size. In C code, I believe that this would be accomplished with a simple malloc, but this type of methodology may not be useful in Fortran.

这里出了什么问题,我该如何解决?我在 GNU/Linux 上使用 gfortran 编译器,所以我认为可以使用所有受支持的语言功能.

What is going wrong here, and how might I fix it? I am using the gfortran compiler on GNU/Linux, so I think that it would be possible to use all of the supported language features.

这是我的测试程序:

program test
implicit none
    integer num1, num2

    print *, 'Starting...'
    num1 = 10
    num2 = 11
    call sub(num1, num2)
    print *, 'Done.'

end program



subroutine sub(num1, num2)
    integer num1, num2
    integer num3

    num3 = num1 + num2 - 1
    integer A(num3)

    do i = 1,num3
        A(i) = i
    end do

    print *, 'Now printing out vector'

    do i = 1,num3
        print *, A(i)
    end do
end subroutine

这是用于编译我的简单测试程序的 cmake 脚本:

Here is the cmake script being used to compile my simple test program:

cmake_minimum_required (VERSION 2.6)
project (test Fortran)

add_executable( test
test.f90
) # end

编译此程序时,我收到以下错误:

When compiling this program, I receive the following error:

/media/RESEARCH/SAS2-version2/test-Q-filter/test-Fcreation/test.f90:20.16:

 integer A(num3)
                1
Error: Unexpected data declaration statement at (1)
/media/RESEARCH/SAS2-version2/test-Q-filter/test-Fcreation/test.f90:23.10:

  A(i) = i
          1
Error: Unexpected STATEMENT FUNCTION statement at (1)
make[2]: *** [CMakeFiles/test.dir/test.f90.o] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2

推荐答案

问题是,因为你在普通语句后面放了一个数据声明语句.

The problem is, because you placed a data declaration statement after a normal statement.

您不必使用用户定义的动态分配,所谓的自动分配就足够了.(它也适用于 C99 AFAIK,但仅适用于堆栈分配).直接替换

You do not have to use user defined dynamic allocation, the so called automatic allocation is enough. (It works also in C99 AFAIK, but only for stack allocations). Just replace

num3 = num1 + num2 - 1
integer A(num3)

integer A(num1 + num2 - 1)
integer num3

num3 = num1 + num2 - 1

.

不幸的是,你不能只写

integer :: num3 = num1 + num2 - 1

因为变量会隐含地 SAVE 并且 num1 和 num2 必须在编译时知道.

because the variable would be implicitly SAVE and num1 and num2 would have to be known at compile time.

注意我没有检查其他错误.

Note I didn't check for other errors.

作为一个完全不同的问题,我建议您对所有子例程使用 mudules.在这种简单的情况下,也可以使用内部子程序.这样您就有了一个明确的界面,您可以检查调用的一致性并使用更高级的功能.

As a completely different issue I recommend you to use mudules for all your subroutines. In this simple case also an internal subroutine one would do. You have an explicit interface then and you can check for consistency of your calls and use more advanced features.

这篇关于创建数组时在 Fortran 中出现意外的数据声明错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆