错误:具有非零等级的part-ref右侧的部分名称具有ALLOCATABLE属性 [英] Error: The part-name to the right of a part-ref with nonzero rank has the ALLOCATABLE attribute

查看:209
本文介绍了错误:具有非零等级的part-ref右侧的部分名称具有ALLOCATABLE属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用子程序sum_real访问数组中派生类型的数组元素。那就是:总结所有人的体重第一项。

  type my_type 
real,dimension(:),allocatable :: weight
real :: total_weight
结束类型my_type

type(my_type),dimension(:),allocatable :: people
type(my_type):: answer

allocate(people (2))
分配(人(1)%权重(2))
分配(人(2)%权重(2))

人(1)%权重(1)= 1
people(2)%weight(1)= 1
people(1)%weight(2)= 3
people(2)%weight(2)= 3

call sum_real(people(:)%weight(1),answer%total_weight)

我想要做的与派生类型数组中的答案类似:select entry ,除了我有一个数组派生类型而不是单个元素的分配数组。



但是,我得到一个编译器错误:

 错误#7828:part-具有非零等级的ref具有ALLOCATABLE属性(6.1.2)。 [WEIGHT] 


解决方案

组件是可分配的。引用( 6.1.2 )实际上是对官方标准文档的引用,它禁止这一点。

原因很简单,可分配的组件(标量或数组)存储在与派生类型本身不同的内存部分中。因此,如果你写了

 总和(人物%total_weight)

 人物%total_weight = 0 

这是没有问题的, total_weight 不是可分配的,它存储在派生类型,编译器只是进入一个简单的循环,并将一个接一个的字段设置为零。您可以事先知道每个%totalweight 的地址。



另一方面

 总和(人物%权重)

  people%weight = 0 

每个%权重存储在其他地方,并且您没有任何简单公式来计算每个 %weight(i)



解决方法是,如果可能的话,修复数组的大小。 b

  real,dimension(2):: weight 

或者使用do循环

$ $ $ $ $ $ $ $ $ $ $ $
S = S + sum(人(i)%重量)
结束做


I would like to access the the elements of an array in a an arrayed derived type using the subroutine sum_real. That is: sum over first entry in the weight for all people.

type my_type
   real, dimension(:), allocatable :: weight
   real :: total_weight
end type my_type

type (my_type), dimension (:), allocatable :: people
type (my_type) :: answer

allocate (people (2))
allocate (people (1)%weight(2))
allocate (people (2)%weight(2))

people (1) % weight(1) = 1
people (2) % weight(1) = 1
people (1) % weight(2) = 3
people (2) % weight(2) = 3

call sum_real ( people (:) % weight(1), answer % total_weight )

What I want to do is similar to the answer found in Array of derived type: select entry, except for the fact that I have an allocated array in an arrayed derived type instead of an single element.

But, I get a compiler error:

error #7828: The part-name to the right of a part-ref with nonzero rank has the ALLOCATABLE attribute (6.1.2).   [WEIGHT]

解决方案

What you try is not possible if your component is allocatable. The reference (6.1.2) is actually a reference to the official standard documents, which prohibits this.

The reason is simple, the allocatable components (scalar or arrays) are stored in a different part of memory than the derived type itself. Therefore if you write

sum(people%total_weight)

or

people%total_weight = 0

it is no problem, total_weight is not allocatable, it is stored within the derived type and the compiler just goes in a simple loop and sets one field after another to zero. You can know the address of each %totalweight beforehand.

On the other hand

sum(people%weight)

or

people%weight = 0

each %weight is stored elsewhere and you don't have any simple formula to compute where is each %weight(i).

The solution is either, to fix the size of the array, if possible

real, dimension(2) :: weight

or use a do loop

s = 0
do i = 1, size(people)
  S = S + sum(people(i)%weight)
end do

这篇关于错误:具有非零等级的part-ref右侧的部分名称具有ALLOCATABLE属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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