函数的返回类型不匹配 [英] Return type mismatch of function

查看:122
本文介绍了函数的返回类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用函数对角fortran90中的复杂矩阵.这是我使用的功能

I want to diagonalize a complex matrix in fortran90 with a function. This is the function I use

!==========================================================================
function inv(A,n)
  Implicit none
  integer :: n  
  complex*16, dimension(n,n):: A
  complex*16, dimension(n,n):: inv
  complex*16,allocatable,dimension(:)::WORK
  integer,allocatable,dimension(:)::IPIV
  integer i,j,info,error

  allocate(WORK(n),IPIV(n),stat=error)
  if (error.ne.0)then
    print *,"error:not enough memory"
    stop
  end if


  call ZGETRF(n,n,A,n,IPIV,info)
  if(info .eq. 0) then
    write(*,*)"succeded"
  else
   write(*,*)"failed"
  end if

  call ZGETRI(n,A,n,IPIV,WORK,n,info)
  if(info .eq. 0) then
    write(*,*)"succeded"
    inv=A
  else
   write(*,*)"failed"
  end if
  !deallocate(A,IPIV,WORK,stat=error)
  !if (error.ne.0)then
  !  print *,"error:fail to release"
  !  stop
  !end if
end function inv

我简单地用

Wmattemp=inv(Wmattemp,nsit)

其类型为

complex*16, allocatable :: Wmattemp(:,:)

但是当我用

gfortran -fdefault-real-8 code.f90 -llapack

code.f90:217.19:

       Wmattemp=inv(Wmattemp,nsit)
               1
 Error: Return type mismatch of function 'inv' at (1) (INTEGER(4)/COMPLEX(8))
 code.f90:217.16:

       Wmattemp=inv(Wmattemp,nsit)
            1
Error: The reference to function 'inv' at (1) either needs an explicit INTERFACE or the rank is incorrect

我不是Fortran专家,所以我找不到问题所在.

II'm not a fortran expert so I cannot find what is the problem.

现在我已添加到主程序

complex*16, allocatable :: inv(:,:)

但我收到此错误

code.f90:217.13:

       A=inv(Wmattemp,nsit)
             1
Error: Array index at (1) must be of INTEGER type, found COMPLEX

推荐答案

由于未使用模块,因此需要在主程序中使用接口块声明函数inv的返回值:

Since you are not using modules, you need to declare the return value of the function inv in the main program using an interface block:

program main
! [...]
interface inv
  function inv(A,n)
    integer :: n  
    complex*16, dimension(n,n):: A
    complex*16, dimension(n,n):: inv
  end function
end interface
! [...]
Wmattemp=inv(Wmattemp,nsit)

这篇关于函数的返回类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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