Error when adding vectors

If you actually want to add instances of your type, you will have to tell Julia how addition is supposed to work for them. Something like:

julia> v1=[Mystruct(1,[1,2],1)]
1-element Vector{Mystruct}:
 Mystruct(1, [1, 2], 1)

julia> v2=[Mystruct(1,[3,4],1)]
1-element Vector{Mystruct}:
 Mystruct(1, [3, 4], 1)

julia> +(x::Mystruct, y::Mystruct) = Mystruct(x.a + y.a, x.b .+ y.b, x.c + y.c)
+ (generic function with 209 methods)

julia> v1+v2
1-element Vector{Mystruct}:
 Mystruct(2, [4, 6], 2)

(note here I’ve simplified things by replacing the SVector with a plain Vector in the struct definition)

1 Like