如果我的问题很愚蠢,我对朱莉娅很新,请原谅我,
对于exmaple我定义了这样的类型:
type Vector2D
x::Float64
y::Float64
end
和2个对象w和v:
v = Vector2D(3, 4)
w = Vector2D(5, 6)
如果我添加它们会引发这个错误:MethodError: no method matching +(::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
朱利亚0.5版
答案 0 :(得分:6)
正如错误消息所示,您必须告诉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)