Arrays Fortran 2008-类中的数组变量
我有一个类,我需要在其中存储Arrays Fortran 2008-类中的数组变量,arrays,fortran,fortran2008,Arrays,Fortran,Fortran2008,我有一个类,我需要在其中存储neuron\u t和connection\u t类型的对象 !> Class representing a general network type :: net_t private character(:), allocatable :: net_type !< Type of the net integer(kind=integer_4neuro)
neuron\u t
和connection\u t
类型的对象
!> Class representing a general network
type :: net_t
private
character(:), allocatable :: net_type !< Type of the net
integer(kind=integer_4neuro) :: num_of_neurons !< Number of neurons in the net
character(:), allocatable :: training_method !< Used training method
class(neuron_t), allocatable :: neuron_arr(:) !< Array containing all neurons
integer(kind=integer_4neuro), allocatable :: input_neuron_arr(:) !< Array of input neuron indices
integer(kind=integer_4neuro), allocatable :: output_neuron_arr(:) !< Array of output neuron indices
class(connection_t), allocatable :: connection_arr(:) !< Array of all connections
contains
!> Prints information about the network to the standard output.
procedure :: print_info => print_info_impl
!> Saves the network instance to the Fortran binary file
procedure :: save_net_bin => save_net_bin_impl
end type net_t
interface net_t
!> Constructor of net_t class
!! Loads complete info about network from file
!! @param[in] filepath Path to the file with network configuration
!! @return Returns pointer to the new instance of the class net_t
module procedure :: new_net_1
end interface net_t
但我得到了以下错误:
new_obj%neuron_arr(i) = mock_neuron_t()
1
Error: Nonallocatable variable must not be polymorphic in intrinsic assignment at (1) - check that there is a matching specific subroutine for '=' operator
你知道吗,我做错了什么mock\u neuron\u t
扩展了类型neuron\u t
,所以应该可以
编辑:
neuron\u t
和connection\u t
类都是抽象类,因此在添加allocate(new\u obj%neuron\u arr)
后,我现在遇到以下错误:
您不能仅在内在赋值(
=
)中赋值给多态变量。您只能对可分配变量执行此操作,而数组元素即使是可分配数组的一部分,也不是可分配变量。请记住所有数组元素必须具有相同的动态类型
因此,如果您想让neuron\u arr(:)
的不同元素具有不同的类型,这是不允许的
但是您可以将数组分配给某个单一类型,并使用selecttype
typeguard
allocate(new_obj)
allocate(new_obj%neuron_arr) !this allocates it to the declared type neuron_t!
! Init object
select type(a => new_obj%aneuron_arr)
type is(neuron_t)
do i=1,5
a(i) = mock_neuron_t()
end do
end select
但是,如果你总是有一个固定的类型,那么
type(neuron\t)
就足够了吗 非常感谢!老实说,我想,class(neuron\u t)
会将其类型设置为neuron\u t
。。。我不能使用type(neuron\u t)
,因为neuron\u t
是一个抽象类。然后必须将allocate语句中的数组分配到正确的类型,并在选择类型中使用该类型。但我担心你的设计是错误的。数组必须一次分配给一个常见的非抽象类型)alocate(类型::数组(大小))
我已经尝试过了(请参见问题中的编辑2),但我遇到了分段错误。。。我真的不知道,现在出了什么问题…谁知道,我认为这应该是一个新问题,一个新问题。我发现了一个错误-mock_neuron_t构造函数返回的是指针,而不是实例本身。请在你的答案中插入这些信息,我认为这是值得接受的。
allocate(new_obj%neuron_arr)
1
Error: Allocating new_obj of ABSTRACT base type at (1) requires a type-spec or source-expr
new_obj%neuron_arr(i) = mock_neuron_t()
1
Error: Nonallocatable variable must not be polymorphic in intrinsic assignment at (1) - check that there is a matching specific subroutine for '=' operator
allocate(new_obj)
allocate(new_obj%neuron_arr) !this allocates it to the declared type neuron_t!
! Init object
select type(a => new_obj%aneuron_arr)
type is(neuron_t)
do i=1,5
a(i) = mock_neuron_t()
end do
end select