Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop Getting UndevarError:尝试在Julia中定义具有内部构造函数的结构时未定义new_Oop_Constructor_Julia - Fatal编程技术网

Oop Getting UndevarError:尝试在Julia中定义具有内部构造函数的结构时未定义new

Oop Getting UndevarError:尝试在Julia中定义具有内部构造函数的结构时未定义new,oop,constructor,julia,Oop,Constructor,Julia,我是朱莉娅的新手,正在试用我从中复制的一些代码。这应该是在Julia中进行面向对象编程(即类)的一种方法: using Lathe.stats: mean, std struct NormalDistribution{P} mu::Float64 sigma::Float64 pdf::P end function NormalDistribution(x::Array) pdf(xt::Array) = [i = (i-μ) / σ for i in

我是朱莉娅的新手,正在试用我从中复制的一些代码。这应该是在Julia中进行面向对象编程(即类)的一种方法:

using Lathe.stats: mean, std

struct NormalDistribution{P}
    mu::Float64
    sigma::Float64
    pdf::P
end

function NormalDistribution(x::Array)
        pdf(xt::Array) = [i = (i-μ) / σ for i in xt]
        return new{typeof(pdf)}(mean(x), std(x), pdf)
end

x = [5, 10, 15, 20]
dist = NormalDistribution(x)
然而,当我在Jupiter笔记本上运行Julia 1.1.1时,我得到了一个例外:

UndefVarError: new not defined

Stacktrace:
 [1] NormalDistribution(::Array{Int64,1}) at ./In[1]:11
 [2] top-level scope at In[1]:15
我发现了一些内部构造函数方法,它们解释了

一个名为的本地存在的特殊函数,用于创建块类型的对象

(尽管上面链接的
new
文档称它是一个关键字)


我可能复制了错误的代码,但也许有人可以解释如何实现原作者在文章中提出的内容。此外,我还不知道如何在Julia中调试,因此任何指针都值得赞赏。

必须在结构定义体内部定义内部构造函数;这是唯一一个
new
有特殊意义的地方。您的构造函数方法不在结构定义中,
new
只是一个常规(未定义)名称。

意思是
new
关键字只存在于内部构造函数中(与外部构造函数相反)

因此,要么选择外部构造函数,在这种情况下,您希望使用默认构造函数来实际创建新实例:

using Statistics

# First the type declaration, which comes with a default constructor
struct NormalDistribution{P}
    mu::Float64
    sigma::Float64
    pdf::P
end

# Another outer constructor
function NormalDistribution(x::Array)
    μ = mean(x)
    σ = std(x)
    pdf(xt::Array) = [(i-μ) / σ for i in xt]

    # This is a call to the constructor that was created for you by default
    return NormalDistribution(μ, σ, pdf)
end
请注意,原始文章中的
pdf
的定义显然有问题(如果只是因为μ和σ没有定义的话)。我试图修改它,使它有一些意义

一个可能的问题是,任何人都可以定义状态不一致的
正态分布
实例:

julia> NormalDistribution(0., 1., x->x+1)
NormalDistribution{var"#11#12"}(0.0, 1.0, var"#11#12"())
这就是为什么您可能想要实际定义一个内部构造函数,在这种情况下,Julia不向您提供默认构造函数,但您可以访问该特殊的
new
函数,该函数创建您定义的类型的对象:

# A type declaration with an inner constructor
struct NormalDistribution2{P}
    mu::Float64
    sigma::Float64
    pdf::P

    # The inner constructor is defined inside the type declaration block
    function NormalDistribution2(x::Array)
        μ = mean(x)
        σ = std(x)
        pdf(xt::Array) = [(i-μ) / σ for i in xt]

        # Use the `new` function to actually create the object
        return new{typeof(pdf)}(μ, σ, pdf)
    end
end
它的行为方式与具有外部构造函数的结构完全相同,只是这次不再提供默认构造函数:

julia> dist2 = NormalDistribution2(x)
NormalDistribution2{var"#pdf#5"{Float64,Float64}}(12.5, 6.454972243679028, var"#pdf#5"{Float64,Float64}(12.5, 6.454972243679028))

julia> dist2.pdf([1, 2, 3])
3-element Array{Float64,1}:
 -1.781572339255412
 -1.626653005407115
 -1.4717336715588185

# No default constructor provided
julia> NormalDistribution2(0., 1., x->x+1)
ERROR: MethodError: no method matching NormalDistribution2(::Float64, ::Float64, ::var"#9#10")
Stacktrace:
 [1] top-level scope at REPL[13]:1

嗯。看起来你很困惑。因此,该函数应该在结束之前作为内部构造函数包含到外部构造函数中。问题是代码在本文中被划分并缓慢构建,只显示了整个构造函数的一部分。如果您感兴趣,并且喜欢我的博客,我将在我的频道上发布一个关于外部构造函数和内部构造函数的视频,这将是第9部分(我现在正在编辑),也许以视频形式观看会更有意义。

谢谢,这很好地解释了这一点。原始文章中代码的缩进是错误的,因此我的
正态分布
函数结束于外部范围,而不是结构内部。我已请他纠正缩进。他似乎也遗漏了您包含的
μ=..
σ=..
语句。我现在理解了,但为了避免进一步混淆,我认为最好在文章中缩进内部构造函数,以便更明显地看到它在struct声明中。最后可能还会显示完整的可执行脚本。谢谢。我同意,文章中后面的内部构造函数定义在结构之外这一事实有点令人困惑。上面的评论可能会有所帮助。
julia> dist2 = NormalDistribution2(x)
NormalDistribution2{var"#pdf#5"{Float64,Float64}}(12.5, 6.454972243679028, var"#pdf#5"{Float64,Float64}(12.5, 6.454972243679028))

julia> dist2.pdf([1, 2, 3])
3-element Array{Float64,1}:
 -1.781572339255412
 -1.626653005407115
 -1.4717336715588185

# No default constructor provided
julia> NormalDistribution2(0., 1., x->x+1)
ERROR: MethodError: no method matching NormalDistribution2(::Float64, ::Float64, ::var"#9#10")
Stacktrace:
 [1] top-level scope at REPL[13]:1