gpt4 book ai didi

oop - Fortran 构造函数出错

转载 作者:行者123 更新时间:2023-12-04 23:22:07 29 4
gpt4 key购买 nike

我想扩展一个抽象类型然后用构造函数初始化但是我得到了错误(来自 gfortran)“错误:结构构造函数中没有给出组件‘功能’的初始值设定项!” 第一个模块是

module A_Module
implicit none
type A
double precision :: x,y
end type A
contains
end module A_Module

第二个模块是

module B_module
use A_Module
type, abstract :: B
contains
procedure (Compute_BFeature), deferred :: B_Feat
end type B
abstract interface
function Compute_BFeature(this)
import B
double precision, dimension(:), allocatable :: Compute_BFeature
class (B)::this
end function Compute_BFeature
end interface
type, extends(B) :: B_new
type (A), dimension(2) :: A_list
double precision, dimension(2) :: Feature
contains
procedure :: B_Feat => B_new_Feature
end type B_new

interface B_new
procedure B_new_Constructor
end interface

contains

function B_new_Constructor(this,A_listInput)
type(B_new):: B_new_Constructor
type (A), dimension(2), intent(in) :: A_listInput
B_new_Constructor%A_list = A_listInput
B_new_Constructor%Feature = B_new_Constructor%B_Feat()
end function

function B_new_Feature (this)
double precision, dimension(:) , allocatable :: B_new_Feature
class (B_new) :: this
allocate(B_new_Feature(2))
B_new_Feature(1) = -(this%A_list(2)%y - this%A_list(1)%y)
B_new_Feature(2) = this%A_list(2)%x - this%A_list(1)%x
end function
end module B_module

代替

    B_new_Constructor%Feature = B_new_Constructor%B_Feat()

我也试过用

    B_new_Constructor%Feature(1) = 1.0
B_new_Constructor%Feature(2) = 1.0

但 gfortran 总是返回相同的错误。为了了解问题是否出在我的 B_new_Feature 函数上我试图查看错误是否与函数的可分配语句有关,但是,即使将 Feature 作为 double 标量(并且 B_new_Feature 进行了相应调整),编译器仍然以相同的方式提示。此刻经过多次尝试,我找不到错误是什么,知道吗?

最佳答案

(因为我对 Fortran OOP 特性还是新手,以下答案可能包含一些大错误,所以请小心......使用 gfortran4.8.2 测试)

首先在 B_new_Constructor() 中,this 作为第一个参数给出但没有明确声明(可能是错字)。因为没有implicit none,所以这个隐式this不会出错。另外,据我所知,Fortran 的“构造函数”不是派生类型的成员函数,而是通常指的是一个模块过程,它被默认结构构造函数重载以返回一个新对象。因此,无需将 this 传递给用户定义的构造函数(此处为 B_new_Constructor()),而是返回一个新构造的对象。

有了原代码,主程序是这样的

program main
use B_module, only: A, B_new
type(B_new) :: p, q, r

p = B_new()
print *, "p = ", p

q = B_new( [ A(1.0d0,2.0d0), A(3.0d0,4.0d0) ] )
print *, "q = ", q

r = B_new( [ A(1.0d0,2.0d0), A(3.0d0,4.0d0) ], [ 5.0d0, 6.0d0 ] )
print *, "r = ", r
end

给出类似的错误信息

p = B_new()
1
Error: No initializer for component 'a_list' given in the structure constructor at (1)!

q = B_new( [ A(1.0d0,2.0d0), A(3.0d0,4.0d0) ] )
1
Error: No initializer for component 'feature' given in the structure constructor at (1)!

现在,如果我们在 B_module 的顶部附加 implicit none 并将 B_new_Constructor() 更改为

function B_new_Constructor( A_listInput ) result( ret )
type(A) :: A_listInput( 2 )
type(B_new) :: ret

print *, "modified constructor called (no optional)"
ret% A_list(:) = A_listInput(:)
ret% Feature(:) = ret% B_Feat()
endfunction

对于 p

我们仍然得到同样的错误
p = B_new()
1
Error: No initializer for component 'a_list' given in the structure constructor at (1)!

这可能是因为不带参数的调用没有匹配过程(这里我们假设默认结构构造函数需要两个参数)。处理此问题的一种方法可能是使用 optional 关键字,例如

function B_new_Constructor( A_listInput , featInput ) result( ret )
type(A), optional :: A_listInput( 2 )
double precision, optional :: featInput( 2 )
type(B_new) :: ret

print *, "modified constructor called"
if ( present( A_listInput ) ) ret% A_list(:) = A_listInput(:)
if ( present( featInput ) ) then
ret% Feature(:) = featInput(:)
else
ret% Feature(:) = ret% B_Feat()
endif
endfunction

然后程序运行为

modified constructor called
p = 1.24543954074099760E-312 1.24546058728534379E-312 1.24543953672918456E-312 2.12199579096527232E-314 1.22424062937569107E-312 -4.01181304423092194E-321
modified constructor called
q = 1.0 2.0 3.0 4.0 -2.0 2.0 !! format slightly changed to fit the terminal
modified constructor called
r = 1.0 2.0 3.0 4.0 5.0 6.0

此输出显示给定两个参数,用户定义的构造函数优先于默认结构构造函数。

为了避免optional关键字,我们还可以使用默认初始化器,这样

type A
double precision :: x = 100.0d0, y = 200.0d0
endtype

type, extends(B) :: B_new
type(A) :: A_list(2)
double precision :: Feature(2) = [ 300.0d0, 400.0d0 ]
contains
...
endtype

然后 B_new_Constructor() 的第一个修改版本(没有 optional)也能正常工作

p =    100.0  200.0  100.0  200.0  300.0  400.0
modified constructor called (no optional)
q = 1.0 2.0 3.0 4.0 -2.0 2.0
r = 1.0 2.0 3.0 4.0 5.0 6.0

此输出表明至少为 r 调用了默认结构构造函数。

------------

编辑:如果我们只想允许单参数构造函数(此处为 A_listInput),可能有两种方法。一种是用两个可选参数修改上面的 B_new_Constructor(),包括以下内容以禁止其他情况:

if ( (        present( A_listInput )  .and.        present( featInput )  ) .or. &
( (.not. present( A_listInput )) .and. (.not. present( featInput )) ) ) then
stop "only one arg permitted"
endif

另一种方法是将没有参数或两个参数的构造函数定义为“虚拟”:

function B_new_Constructor_arg1 ( A_listInput ) result( ret )
type(A) :: A_listInput( 2 )
type(B_new) :: ret

ret% A_list(:) = A_listInput(:)
ret% Feature(:) = ret% B_Feat()
endfunction

function B_new_Constructor_arg0 () result( ret )
type(B_new) :: ret
stop "constructor with no argument prohibited"
endfunction

function B_new_Constructor_arg2 ( A_listInput, featInput ) result( ret )
type(A) :: A_listInput( 2 )
double precision :: featInput( 2 )
type(B_new) :: ret
stop "constructor with two arguments prohibited"
endfunction

interface B_new
procedure B_new_Constructor_arg1
procedure B_new_Constructor_arg0 !! this masks default initializers (if any)
procedure B_new_Constructor_arg2 !! this masks default structure constructor
endinterface

这两种方法似乎都有效,但都不是很优雅......(希望有更好的方法来做到这一点)。 [很抱歉,我的回答太长了。]

关于oop - Fortran 构造函数出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31036638/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com