Syntax 用于赋值的Fortran语法
Fortran语法快把我逼疯了!有人能解释一下我怎么称呼这个作业吗(我很确定这也不是正确的术语…)。我正在尝试根据值类型分配类型。我有以下资料: module test_module implicit none type :: mytype integer :: i real :: r logical :: l contains generic :: assignment(=) => mytype_to_type procedure, pass(me) :: mytype_to_type end type mytype contains subroutine mytype_to_type(t, me) implicit none class(*), intent(inout) :: t class(mytype), intent(in) :: me !.. process based on input type select type (t) type is (integer) t = me%i type is (real) t = me%r type is (logical) t = me%l class default stop "none" return end select end subroutine mytype_to_type end module test_module program test use test_module implicit none type(mytype) :: t_type integer :: i = 1 real :: r = 1. logical :: l = .true. t_type = i !! how is this supposed to work? select type(t_type) type is (integer) write(*,*) "is int" type is (real) write(*,*) "is real" type is (logical) write(*,*) "is logical" class default return end select end program test 模块测试模块 隐式无 类型::mytype 整数::i real::r 逻辑::l 包含 泛型::赋值(=)=>mytype\u到\u类型 过程,通过(me)::mytype\u到\u类型 结束类型mytype 包含 子例程mytype_到_type(t,me) 隐式无 类别(*),意图(输入输出)::t 类(mytype),意图(in)::我 !.. 基于输入类型的流程 选择类型(t) 类型为(整数) t=me%i 类型为(真实) t=me%r 类型为(逻辑) t=me%l 类默认值 停止“无” 返回 结束选择 结束子例程mytype\u到\u类型 端模块测试模块 程序测试 使用测试单元 隐式无 类型(mytype)::t\u类型 整数::i=1 实数::r=1。 逻辑::l=.true。 t_type=i!!这是怎么回事? 选择类型(t_类型) 类型为(整数) 写入(*,*)“为整数” 类型为(真实) 写(*,*)“是真实的” 类型为(逻辑) 写入(*,*)“是逻辑的” 类默认值 返回 结束选择 结束程序测试 这能奏效吗?有人能帮我吗Syntax 用于赋值的Fortran语法,syntax,fortran,variable-assignment,Syntax,Fortran,Variable Assignment,Fortran语法快把我逼疯了!有人能解释一下我怎么称呼这个作业吗(我很确定这也不是正确的术语…)。我正在尝试根据值类型分配类型。我有以下资料: module test_module implicit none type :: mytype integer :: i real :: r logical :: l contains generic :: assignment(=) => mytype_to
谢谢 在支持已定义赋值的子例程中,两个参数分别对应于赋值语句的左侧和右侧。1 在这里,您提供的子例程是从
my_type
表达式到无限多态对象的赋值。这不是您想要的,请参见左侧的t\u type
相反,您应该为my_type
对象提供定义的赋值
subroutine stuff_to_mytype(me,t)
class(mytype), intent(out) :: me
class(*), intent(in) :: t
!.. process based on input type
select type (t)
type is (integer)
me%i = t
type is (real)
me%r = t
type is (logical)
me%l = t
class default
stop "none"
return
end select
end subroutine stuff_to_mytype
也就是说,您可以为支持的每种类型使用一个特定的子例程,而不是具有通用分辨率的无限多态右侧。在这种情况下,您也可以考虑泛型结构构造函数(<代码> TyType = MyType(I)< /C> >)。
1准确地说,第二个参数是用括号括起来的右侧。是否有编译器错误?分享吧!您没有任何定义的从integer到my_类型的赋值。只有从我的类型到类(*)。谢谢你的回答!我不太明白,对不起!也许我应该澄清我想做什么:我想返回一个基于输入类型的类型。