Fortran的子程序指针不匹配的阵列维度 [英] Fortran Subroutine Pointers for Mismatching Array Dimensions

查看:166
本文介绍了Fortran的子程序指针不匹配的阵列维度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与Fortran和功能/子程序指针的一个问题。我有采取数组作为参数两种功能。在F1是一个(N,N),在f2的它是一个(n * n的)。当我手动调用子程序,我可以用同一个阵列做到这一点:

I'm having a problem with Fortran and function/subroutine pointers. I have two functions that take an array as an argument. In f1 it is a(n,n), in f2 it's a(n*n). When I call the subroutine manually, I can do this with the same array:

real :: a(5, 5)
call f1(a, 5)
call f2(a, 5)

但是,当我尝试用一​​个指针要做到这一点,编译与此错误抛出回我:

But when I try to do this with a pointer, the compiler throws it back at me with this error:

ptr => f2
       1
Error: Interface mismatch in procedure pointer assignment at (1): Type/rank missmatch in argument 'a'

有没有办法解决?我在想三分球,但我有同样的问题,创造它,我需要知道的维数。结果

Is there a way around this? I was thinking about pointers, but there I have the same problem, to create it I need to know the number of dimensions.

有关参考,下面是完整的code(我希望这不是太长..)

For reference, here's the complete code (I hope it's not too long..)

program ptrtest
implicit none

interface
    subroutine fnc(a, n)
        integer :: n
        real :: a(n, n)
    end subroutine

    subroutine f1(a, n)
        integer :: n
        real :: a(n, n)
    end subroutine

    subroutine f2(a, n)
        integer :: n

        real :: a(n*n)
    end subroutine
end interface

procedure(fnc), pointer :: ptr => null()
real :: a(5, 5)
real :: b(4, 4)

ptr => f1

call ptr(a, 5)
write(*,*) a

!this one does not work..

!ptr => f2
!
!call ptr(b, 4)
!write(*,*) b

call f2(b, 4)
write(*,*) b
end program

subroutine f1(a, n)
integer :: n
real :: a(n, n)
integer :: i

a = 1
end subroutine

subroutine f2(a, n)
integer :: n
real :: a(n*n)

a = 2
end subroutine

我真的很希望有办法做到这一点。我真的不能重写所有子程序所以数组的大小,每次匹配:/结果

I really hope there is a way to do this. I can't really rewrite all the subroutines so the dimensions of the array match every time :/

问候,卡巴

推荐答案

如果我使用显式接口(通过接口块)使用隐式接口(通过过程声明没有提及的接口),改变你的范例程序,它似乎为我工作。

If I change your example program from using explicit interfaces (through the interface block) to using implicit interfaces (through procedure declarations without mentioning an interface), it appears to work for me.

所以删除接口块,稍微改变 PTR 的声明,并添加过程声明为 F1 F2 ,就像这样:

So remove the interface block, slightly alter the declaration of ptr, and add procedure declarations for f1 and f2, like this:

procedure(), pointer :: ptr => null()
procedure() :: f1, f2

(或者,您也可以使用外部语句 F1 F2 ,而不是过程语句。)

(Alternatively, you can use an external statement for f1, and f2, instead of the procedure statement.)

我不知道如何可行的,这是为真正的计划,因为如果实际的子程序使用的一些Fortran 90中引入的功能,以后你可能需要显式接口。

I don't know how feasible this is for the real program, because you may need the explicit interfaces if the actual subroutines use some of the features introduced in Fortran 90 and later.

这篇关于Fortran的子程序指针不匹配的阵列维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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