qwebfub3i4u
qwebfub3i4u

Reputation: 319

Julia: Error passing user-defined struct to functions

I have these structures defined as:

struct CLocation{T<:Real}
  x::Array{T,1}
  y::Array{T,1}
  z::Array{T,1}
end

struct CSize{T<:Real}
  x::Array{T,1}
  y::Array{T,1}
  z::Array{T,1}
end

struct FLocation{T<:Real}
  x::Array{T,1}
  y::Array{T,1}
  z::Array{T,1}
end

struct MStructure
  dimension::Real
  dims::Array{Int,1}
  csize::CSize
  ccenters::CLocation
  fcenters::FLocation
  corner::Array{Int,1}
  edge::Array{Int,1}
end

struct CValue # {T<:Real}
  domain::MStructure
  value::Union{Array{<:Real}, DenseArray{Bool}} #Union{Array{T}, BitArray{}}
end

Where the CValue.value entry stores the numerical values of interest to me.

I have 3 variables that use this type, that are defined as:

c_new = Vector{CValue}(undef,N)
x_new = Vector{CValue}(undef,N)
c_tot::CValue

Where c_tot is simply of type CValue, and c_new and x_new are vectors of size N with each entry of the vector being type CValue.

I'd like to pass these variables through 2 operations that I hope to convert to functions:

x_new = [c_new[i]/c_tot for i in 1:N]

My intention is that this function divides every numerical value in c_new[i].value by the corresponding value in c_tot.value, giving a vector x_new of size N.

and

c_tot = [c_tot + c_new[i] for i in 1:N]

Which I would like to sum all the values for every c_new[i].value, giving a variable c_tot of type CValue

The error I have received is:

ERROR: LoadError: MethodError: no method matching /(::CValue, ::Vector{CValue})

Upvotes: 0

Views: 106

Answers (1)

Benoit Pasquier
Benoit Pasquier

Reputation: 3015

You have to define operations on custom types that you create. Here I think you'd need to define something like

Base.:/(a::CValue, b::CValue) = a.value / b.value

(Note that it's easier to help you if you can provide copy-pastable code that reproduces the error.)

Upvotes: 1

Related Questions