我对朱莉亚很陌生,如果我的问题很愚蠢,请原谅我,
对于 exmaple,我定义了这样的类型:
type Vector2D
x::Float64
y::Float64
end
和 2 个对象 w 和 v:
v = Vector2D(3, 4)
w = Vector2D(5, 6)
如果我把它们加起来,就会出现这个错误: MethodError: no method matches +(::Vector2D,::Vector2D)
没关系,但是当我想定义一个方法时
总结这些对象
+(a::Vector2D, b::Vector2D) = Vector2D(a.x+b.x, a.y+b.y)
它引发此错误:
error in method definition: function Base.+ must be explicitly imported to be extended
Julia 版本0.5
最佳答案
正如错误消息所示,您必须告诉 Julia 您想要从 Base(标准库)扩展 +
函数:
import Base: +, -
+(a::Vector2D, b::Vector2D) = Vector2D(a.x + b.x, a.y + b.y)
-(a::Vector2D, b::Vector2D) = Vector2D(a.x - b.x, a.y - b.y)
关于julia - 函数 Base.+ 必须显式导入才能扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42888911/