我很难为我的结构 PERK
编写正确的构造函数。给我带来麻烦的是对我的成员数组使用 new()
。代码如下所示:
abstract type PERKTYPE end
struct PERK <: PERKTYPE
NumStages::Int
ACoeffs::Array{BigFloat}
c::Array{BigFloat}
function PERK()
ACoeffsFile = open("some/existing/file.txt", "r")
NumStages = countlines(ACoeffsFile)
close(ACoeffsFile)
#println(NumStages)
ACoeffs = Array{BigFloat}(undef, NumStages, 2)
# Fille ACoeffs with data from file, omitted here
c = Array{BigFloat}(undef, NumStages)
for i in 1:NumStages
c[i] = (i-1)/(2*(NumStages-1))
end
new(NumStages) # Fine
new(ACoeffs, c) # Not working if un-commented
end # PERK()
end # struct PERK
我收到错误
ERROR: LoadError: MethodError: Cannot `convert` an object of type Matrix{BigFloat} to an object of type Int64
Closest candidates are:
convert(::Type{T}, ::LLVM.GenericValue) where T<:Signed at ~/.julia/packages/LLVM/gE6U9/src/execution.jl:27
convert(::Type{T}, ::LLVM.ConstantInt) where T<:Signed at ~/.julia/packages/LLVM/gE6U9/src/core/value/constant.jl:89
convert(::Type{T}, ::Ptr) where T<:Integer at ~/Software/julia-1.7.2/share/julia/base/pointer.jl:23
...
Stacktrace:
[1] PERK()
@ Trixi ~/.../methods_PERK.jl:26
这是怎么回事?当然,我对数组的任何转换根本不感兴趣。
最佳答案
我在这里可能是错的,但是new()
创建一个 PERK 结构。意思是具有三个参数的结构。如果您调用new()
只需一个 NumStages
没关系,因为它是结构的第一个参数。然而
new(ACoeffs, c)
尝试输入数组ACoeffs
进入NumStages
这会产生您看到的错误。
您可以使用以下方法克服这个问题
new(NumStages, ACoeffs, c)
希望这会有所帮助。
关于julia - new() 用于 Julia 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72401847/