无法将初始值分配给模块中的派生数据类型 [英] Cannot assign initial value to derived data type in a module

查看:192
本文介绍了无法将初始值分配给模块中的派生数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Fortran 模块中,我试图将初始值分配给派生数据类型,其组件是一个过程指针,但得到一个错误消息:意外的指针分配。



模块中,如何将初始值赋给包含过程指针的派生类型?

 模块pointer_mod 

使用legendrePolynomials
隐式无

接口
函数func(z)
real * 8 :: func
real * 8,intent(in):: z
结束函数func
结束接口

类型proc_ptr
过程(func),指针,nopass :: f_ptr
结束类型proc_ptr

类型(proc_ptr),维度(6):: basis

basis(1)%f_ptr => Legendre0!或基础(1)%f_ptr => null()

结束模块pointer_mod

其中:

$ Legendre0(x)result(y)
real,intent(in):: x
real :: y

  b $ by = 1 
end function


解决方案

您会收到错误消息,因为您在任何子例程之外发出指针分配,通常只发生声明。将这个赋值放在一个子程序中(见下面)表明,如果你确定, Legendre0()函数也可以使用real * 8类型来匹配接口声明(为了测试的目的,我也把Legendre函数放在同一个模块中):

  module pointer_mod 
隐式无

接口
函数func(z)
real * 8 :: func
real * 8,intent(in):: z
end函数func
结束接口

类型proc_ptr
过程(func),指针,nopass :: f_ptr
结束类型proc_ptr

类型proc_ptr),dimension(6):: basis


包含

子例程test()
basis(1)%f_ptr => Legendre0!或基础(1)%f_ptr => ()x













$ b $ * :y
y = 1
结束函数Legendre0

结束模块pointer_mod

作为附加评论:您应该考虑声明您的实际变量,如

  integer,parameter :: dp = kind(1.0d0)
real(dp):: whatever

而不是 real * 8 符号,这是obsolate。


In a Fortran module, I'm trying to assign initial value to a derived data type whose component is a procedure pointer, but get an error message: unexpected pointer assignment.

In a module, how to assign initial value to a derived type containing a procedure pointer?

    module pointer_mod  

    use legendrePolynomials
    implicit none

    interface
      function func (z)
      real*8 :: func
      real*8, intent (in) :: z
      end function func
    end interface

    type proc_ptr
      procedure (func), pointer, nopass :: f_ptr
    end type proc_ptr

    type(proc_ptr), dimension(6) :: basis

    basis(1) % f_ptr => Legendre0 ! or basis(1) % f_ptr => null()

    end module pointer_mod   

where:

    function Legendre0(x) result(y)
    real, intent(in) :: x
    real :: y
    y = 1
    end function

解决方案

You get the error message, because you issue an pointer assignment outside of any subroutines, where usually only declarations should occur. Putting the assignment in a subroutine (see below) shows, that things work perfectly, provided you make sure, the Legendre0() function also use real*8 type in order to match the interface declaration (for testing purposes, I also put the Legendre function in the same module):

module pointer_mod  
  implicit none

  interface
    function func (z)
      real*8 :: func
      real*8, intent (in) :: z
    end function func
  end interface

  type proc_ptr
    procedure (func), pointer, nopass :: f_ptr
  end type proc_ptr

  type(proc_ptr), dimension(6) :: basis


contains

  subroutine test()
    basis(1)%f_ptr => Legendre0 ! or basis(1) % f_ptr => null()
  end subroutine test

  function Legendre0(x) result(y)
    real*8, intent(in) :: x
    real*8 :: y
    y = 1
  end function Legendre0

end module pointer_mod

As an additional comment: You should consider to declare your real variables like

integer, parameter :: dp = kind(1.0d0)
real(dp) :: whatever

instead of the real*8 notation, which is obsolate.

这篇关于无法将初始值分配给模块中的派生数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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