最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

fortran运算符重载(=)

SEO心得admin51浏览0评论
本文介绍了fortran运算符重载(=)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有一种方法可以重载=运算符,以便您可以像以下示例中那样编写赋值:

Is there a way to overload the = operator so that you can write an assignment like in this example:

module constants_mod integer,parameter :: dpn = selected_real_kind(14) end module module vectorField_mod use constants_mod implicit none private public :: vectorField public :: allocateX,allocateY,allocateZ public :: delete ! public :: operator(=) type vectorField integer,dimension(3) :: sx,sy,sz real(dpn),dimension(:,:,:),allocatable :: x,y,z end type interface delete module procedure deallocateVectorField end interface ! interface operator (=) ! module procedure vectorAssign ! end interface contains ! function vectorAssign(f) result(q) ! implicit none ! real(dpn),intent(in) :: f ! type(vectorField) :: q ! q%x = f; q%y = f; q%z = f ! end function ! subroutine vectorAssign(f,g) ! implicit none ! type(vectorField),intent(inout) :: f ! real(dpn),intent(in) :: g ! f%x = g; f%y = g; f%z = g ! end subroutine subroutine allocateX(field,Nx,Ny,Nz) implicit none type(vectorField),intent(inout) :: field integer,intent(in) :: Nx,Ny,Nz if (allocated(field%x)) deallocate(field%x) allocate(field%x(Nx,Ny,Nz)) field%sx = shape(field%x) end subroutine subroutine allocateY(field,Nx,Ny,Nz) implicit none type(vectorField),intent(inout) :: field integer,intent(in) :: Nx,Ny,Nz if (allocated(field%y)) deallocate(field%y) allocate(field%y(Nx,Ny,Nz)) field%sy = shape(field%y) end subroutine subroutine allocateZ(field,Nx,Ny,Nz) implicit none type(vectorField),intent(inout) :: field integer,intent(in) :: Nx,Ny,Nz if (allocated(field%z)) deallocate(field%z) allocate(field%z(Nx,Ny,Nz)) field%sz = shape(field%z) end subroutine subroutine deallocateVectorField(field) implicit none type(vectorField),intent(inout) :: field deallocate(field%x,field%y,field%z) field%sx = 0; field%sy = 0; field%sz = 0 end subroutine end module program test use constants_mod use vectorField_mod implicit none type(vectorField) :: a integer :: N = 1 real(dpn) :: dt = 0.1 call allocateX(a,N,N,N) call allocateY(a,N,N,N) call allocateZ(a,N,N,N) a%x = dble(1.0) ! want to avoid this a%y = dble(1.0) ! want to avoid this a%z = dble(1.0) ! want to avoid this a = real(1.0,dpn) ! want this instead (does not compile) call delete(a) end program

我尝试了两种不同的方法(如注释所示),但是我收到错误消息,指出通用规范中存在语法错误(用于公开=运算符).在此方面的任何帮助将不胜感激!

I've tried two different ways (shown in comments) but I get errors saying that there is a syntax error in generic specification (for publicizing the = operator). Any help on doing this is greatly appreciated!

推荐答案

对于已定义的分配,operator(=)不正确,但assignment(=)是:请参见Fortran 2008 12.4.3.4.3.因此,您需要两个结块

For defined assignment operator(=) is not correct, but assignment(=) is: see Fortran 2008 12.4.3.4.3. So you instead want the two lumps

public :: assignment (=)

interface assignment (=) module procedure vectorAssign end interface

请注意,定义分配的正确方法是使用子例程(尽管受让人可以使用intent(out)而不是intent(inout)).

Note that the correct way to define the assignment is by the subroutine as you have it (although the assignee could have intent(out) instead of intent(inout)).

发布评论

评论列表(0)

  1. 暂无评论