愤怒的三炮 发表于 2023-3-25 02:04:05

Type 包含 allocatable 数组

可分配数组中的元素是type类型,而元素中又包含可分配数组,该怎么解决呢?
    type mytype
      sequence
      integer,dimension(:),allocatable:: array_type
      integer,dimension(10):: other_variables
    end type
   
    type(mytype),dimension(:),allocatable:: array_main
   
    allocate(array_main%array_type(10))
   
    allocate(array_type(10))

    end
提示错误:
Component to the right of a part reference with nonzero rank must not have the ALLOCATABLE attribute

唐汉 发表于 2023-3-25 06:46:00

program main

   implicit none

   type :: mytype
      sequence
      integer, dimension(:), allocatable :: array_type
      integer, dimension(10) :: other_variables
   end type mytype

   type(mytype), dimension(:), allocatable :: array_main
   integer :: i

   allocate (mytype :: array_main(10))
   do i = 1, size(array_main)
      allocate (array_main(i)%array_type(10))
   end do

end program main

不晓得是不是正确理解了你的问题,但是应该只是格式问题。

kyra 发表于 2023-3-25 08:00:30

根据我的习惯,做了一些修改。

我的有些习惯不是必须的,但我个人认为会让更清晰。

type T_mytype !类型名做个区分
sequence
integer,allocatable:: array_type(:) !非必要不写dimension
integer:: other_variables(10)
end type T_mytype
!必要写 dimension 的情况是,需要大量定义相同大小尺寸的多个数组
type(T_mytype),allocatable:: array_main(:)
integer :: j
allocate(array_main(3)) !先分配array_main
do j = 1 , size(array_main)
allocate(array_main(j)%array_type(10)) !再逐个分配array_type
end do
!allocate(array_type(10)) !这句多余,且无意义

end

weixing1531 发表于 2023-3-25 12:08:31

二维不等长数组的固定套路

愤怒的三炮 发表于 2023-3-25 22:54:49

唐汉 发表于 2023-3-25 06:46
program main

   implicit none


谢谢你!原来它需要先分配main里面的数组长度,然后再分配type里元素的长度,我顺序搞反了。

愤怒的三炮 发表于 2023-3-25 22:57:36

kyra 发表于 2023-3-25 08:00
根据我的习惯,做了一些修改。

我的有些习惯不是必须的,但我个人认为会让更清晰。


很受用,谢谢!
我是想读地震记录的,想着先确定好每一道的数组大小(type里的数组元素),然后再分配总共有多少地震道。
看来计算机的逻辑并不是这样的。

愤怒的三炮 发表于 2023-3-25 22:59:54

weixing1531 发表于 2023-3-25 12:08
二维不等长数组的固定套路

是的,本质就是一个二维的不等长数组。

愤怒的三炮 发表于 2023-3-26 01:48:57

本帖最后由 愤怒的三炮 于 2023-3-26 02:04 编辑

kyra 发表于 2023-3-25 08:00
根据我的习惯,做了一些修改。

我的有些习惯不是必须的,但我个人认为会让更清晰。

我想用这个派生类型数组去读取SEGY文件,但是提示错误
type trace
      sequence
      type(segy_trace_head):: header
      real(4),dimension(:),allocatable:: idata
end type

type(segy_reel_head):: reel_head
type(trace),dimension(:),allocatable:: traces
integer:: fp,fid_segy,fid_traces
integer:: i
   
open(newunit=fid_segy,file='D:/Example.SEGY',form='unformatted',access='stream',convert='big_endian')
   
allocate(traces(500))
do i = 1,500
   allocate(traces(i)%idata(6000))
end do
   
read(fid_segy) reel_head
   
do i = 1, 500
      
   read(fid_segy) traces(i) ! 读取前500道
   
end do

Error: Data transfer element at (1) cannot have ALLOCATABLE components unless it is processed by a defined input/output procedure
出错位置在第23行,该怎么处理呢?

kyra 发表于 2023-3-26 11:42:18

改成
read(fid_segy) traces(i)%header ,traces(i)%idata ! 读取前500道
就行了。
带有可分配数组的 type,不能整体读写。

风平老涡 发表于 2023-3-26 11:50:25

本帖最后由 风平老涡 于 2023-3-26 11:53 编辑

愤怒的三炮 发表于 2023-3-26 01:48
我想用这个派生类型数组去读取SEGY文件,但是提示错误
type trace
      seque ...
read语句不能直接读取type类变量。


do i = 1, 500
read(fid_segy) traces(i) % header, (traces(i) % idata(j), j = 1, 6000)
end do
页: [1] 2
查看完整版本: Type 包含 allocatable 数组