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

查看:341
本文介绍了创建数组时,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.

作为一个完全不同的问题,我建议您对所有子例程使用mudule.在这种简单的情况下,内部子例程也可以执行.这样便有了一个显式的界面,可以检查呼叫的一致性并使用更高级的功能.

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天全站免登陆