【发布时间】:2015-01-13 03:05:33
【问题描述】:
我遇到了这篇文章中详述的函数的错误。
出现问题是因为我试图返回与 输入类型。任何人都可以提出解决方案吗?我最初对每个都有一个功能 类型,然后是通用接口将它们分组为相同的名称。现在我正在尝试 使用多态性将所有内容放在一个函数中。
这是 gfortran 给我的错误。
gfortran -o build/lib/foul.o -c -ffree-form -g -J./build/lib lib/foul.f
lib/foul.f:471.45:
Function cassign (expr, a, b, wrn) Result (c)
我尝试使用可分配数组。然后在主程序中做
Character (len=65), Allocatable :: sc(:)
Integer, Allocatable :: ic(:)
Real, Allocatable :: rc(:)
Allocate (sc(1))
Allocate (ic(1))
Allocate (rc(1))
sc = cassign (ra < rb, sa, sb)
ic = cassign (ra < rb, ia, ib)
rc = cassign (ra < rb, ra, rb)
这会返回以下错误
gfortran -o build/utests/test_foul.o -c -ffree-form -g -J./build/lib utests/test_foul.f utests/test_foul.f:315.7:
sc = cassign (ra < rb, sa, sb)
1
Error: Can't convert CLASS(*) to CHARACTER(1) at (1)
utests/test_foul.f:316.7:
ic = cassign (ra < rb, ia, ib)
1
Error: Can't convert CLASS(*) to INTEGER(4) at (1)
utests/test_foul.f:317.7:
rc = cassign (ra < rb, ra, rb)
1
Error: Can't convert CLASS(*) to REAL(4) at (1)
1
Error: CLASS variable 'c' at (1) must be dummy, allocatable or pointer
lib/foul.f:495.10:
c = a
1
Error: Nonallocatable variable must not be polymorphic in intrinsic
assignment at (1) - check that there is a matching specific subroutine
for '=' operator
lib/foul.f:497.10:
c = b
1
这是我编写的函数。变量a 和b 可以是任何类型
字符、整数或实数。并且输出类型应该匹配输入 a 和 b
如果两种类型匹配,函数type_match (a, b) 返回真,否则返回假。
Function cassign (expr, a, b, wrn) Result (c)
Logical, Intent(in) :: expr
Class (*), Intent(in) :: a, b
Logical, Intent (out), Optional :: wrn
Class (*) :: c
Logical :: warn, tma, tmb
!!$ Perform warning tests (performs type matching).
If (Present (wrn)) Then
!!$ Matching input types.
tma = type_match (a, b)
if (tma) Then
tmb = type_match (a, c)
!!$ Matching input and output types.
If (tmb) Then
If (expr) Then
c = a
Else
c = b
End If
wrn = .False.
!!$ Warning: Non-matching types.
Else
wrn = .True.
End If
Else
wrn = .True.
End If
Else
If (expr) Then
c = a
Else
c = b
End If
End If
End Function cassign
【问题讨论】:
-
您是否尝试过使
c可分配(如错误消息所示)?或者您是否试图不这样做,以便您可以以有趣的方式使用结果? -
我使用的是最新的 gfortran 4.9。
-
lib/foul.f:497.10: c = a 1 Error: Assignment to an allocatable polymorphic variable at (1) is not yet supported -
它似乎有效。但是我必须返回一个数组而不仅仅是一个变量。
-
gfortran -o build/utests/test_foul.o -c -ffree-form -g -J./build/lib utests/test_foul.f utests/test_foul.f:315.7: sc = cassign (ra < rb, sa, sb) 1 Error: Can't convert CLASS(*) to CHARACTER(1) at (1)
标签: function class return fortran