Arrays 错误:秩非零的零件引用右侧的零件名称具有可分配属性
我想使用子例程sum_real访问数组派生类型中的数组元素。也就是说:所有人的第一项重量总和Arrays 错误:秩非零的零件引用右侧的零件名称具有可分配属性,arrays,fortran,derived-types,Arrays,Fortran,Derived Types,我想使用子例程sum_real访问数组派生类型中的数组元素。也就是说:所有人的第一项重量总和 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)) al
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 )
我想做的与中的答案类似,只是在数组派生类型中分配了一个数组,而不是单个元素
但是,我得到一个编译器错误:
error #7828: The part-name to the right of a part-ref with nonzero rank has the ALLOCATABLE attribute (6.1.2). [WEIGHT]
如果您的组件是可分配的,那么您尝试的是不可能的。参考文件(
6.1.2
)实际上是对官方标准文件的参考,这是禁止的
原因很简单,可分配组件(标量或数组)存储在与派生类型本身不同的内存部分。所以如果你写
sum(people%total_weight)
或
没问题,总权重
是不可分配的,它存储在派生类型中,编译器只是在一个简单的循环中运行,并将一个字段接着一个字段设置为零。您可以事先知道每个%totalweight
的地址
另一方面
sum(people%weight)
或
每个%weight
都存储在别处,您没有任何简单的公式来计算每个%weight(i)
的位置
如果可能的话,解决方案是固定数组的大小
real, dimension(2) :: weight
或者使用do循环
s = 0
do i = 1, size(people)
S = S + sum(people(i)%weight)
end do
如果您的组件是可分配的,那么您尝试的是不可能的。参考文件(
6.1.2
)实际上是对官方标准文件的参考,这是禁止的
原因很简单,可分配组件(标量或数组)存储在与派生类型本身不同的内存部分。所以如果你写
sum(people%total_weight)
或
没问题,总权重
是不可分配的,它存储在派生类型中,编译器只是在一个简单的循环中运行,并将一个字段接着一个字段设置为零。您可以事先知道每个%totalweight
的地址
另一方面
sum(people%weight)
或
每个%weight
都存储在别处,您没有任何简单的公式来计算每个%weight(i)
的位置
如果可能的话,解决方案是固定数组的大小
real, dimension(2) :: weight
或者使用do循环
s = 0
do i = 1, size(people)
S = S + sum(people(i)%weight)
end do
如果您有一个F2003编译器,并且组件数组的边界对于一个特定的父数组对象是相同的,那么对于VladimirF指定的常量表达式/使用do循环方法指定的大小,第三种方法是参数化该类型
type my_type(n) ! This type has one parameter - n
integer, len :: n ! The parameter n is a length parameter.
real :: weight(n) ! This component depends on that parameter.
end type my_type
type (my_type(:)), dimension(:), allocatable :: people
! This integer is the size of the people component. Because
! people is allocatable it can be determined at runtime.
number_of_people = 2
! This integer is the size of the weight component that we want
! in the array people. Other arrays and scalars of type
! my_type can have different sizes for that component.
! Because people is allocatable this can be determined at
! runtime.
number_of_weights = 2
allocate( my_type(number_of_weights) :: people(number_of_people) )
! Define people%weight here.
people(1)%weight(1) = 1
...
! Using sum intrinsic for the sake of example
do i = 1, people%n
! The argument to sum is an array section.
print *, sum(people%weight(i))
! ^ ^ Reference to an element of a component
! | Reference to the entire people array
end do
参数化类型数组中的每个元素都具有相同的类型参数,因此people
中的每个weight
组件具有相同的边界,因此people%weight
等引用变为“常规”
使用此方法(或恒定组件大小规范方法)的代码仍然必须遵循组件引用的限制,即只有引用的一部分可以具有非零秩(您不能将人员%weight
作为一个整体使用,因为人员和权重组件都具有秩1)
在可分配组件的情况下,某些元素中的某些组件可能未被分配,而在分配这些组件的位置,组件可能具有不同的边界,这使得跨数组元素“常规”引用组件中的数据在概念上很困难。如果您有F2003编译器,对于特定的父数组对象,组件数组的边界是相同的。对于VladimirF指定的常量表达式/使用do循环方法指定的大小,第三种方法是参数化类型
type my_type(n) ! This type has one parameter - n
integer, len :: n ! The parameter n is a length parameter.
real :: weight(n) ! This component depends on that parameter.
end type my_type
type (my_type(:)), dimension(:), allocatable :: people
! This integer is the size of the people component. Because
! people is allocatable it can be determined at runtime.
number_of_people = 2
! This integer is the size of the weight component that we want
! in the array people. Other arrays and scalars of type
! my_type can have different sizes for that component.
! Because people is allocatable this can be determined at
! runtime.
number_of_weights = 2
allocate( my_type(number_of_weights) :: people(number_of_people) )
! Define people%weight here.
people(1)%weight(1) = 1
...
! Using sum intrinsic for the sake of example
do i = 1, people%n
! The argument to sum is an array section.
print *, sum(people%weight(i))
! ^ ^ Reference to an element of a component
! | Reference to the entire people array
end do
参数化类型数组中的每个元素都具有相同的类型参数,因此people
中的每个weight
组件具有相同的边界,因此people%weight
等引用变为“常规”
使用此方法(或恒定组件大小规范方法)的代码仍然必须遵循组件引用的限制,即只有引用的一部分可以具有非零秩(您不能将人员%weight
作为一个整体使用,因为人员和权重组件都具有秩1)
在可分配组件的情况下,某些元素中的某些组件可能未被分配,并且在分配这些组件的位置,组件可能具有不同的边界,从而形成“常规”在概念上很难跨数组元素引用组件中的数据。权重组件是否总是大小为2?权重组件是否总是大小为2?