hello guys,
I have defined a struct with 5 variables
then
I created one and I passed 3 out of 5 variables
now I am getting error
How do I define a struct without needing to define all variables
Thank you
hello guys,
I have defined a struct with 5 variables
then
I created one and I passed 3 out of 5 variables
now I am getting error
How do I define a struct without needing to define all variables
Thank you
You probably want to define a constructor with default arguments, like:
julia> struct MyType
a
b
c
end
julia> MyType(a, b) = MyType(a, b, "default value")
MyType
julia> MyType(1,2)
MyType(1, 2, "default value")
Okay… I will check it now and give you feedback
Thank you
It didnot work until
I declared my type as “mutable struct”
and
I also had to give all variable a zero value as an initial value
then
I worte
mystruct.x=50
look here please
![p33|690x401](upload://1t5SxtMZj1Dgw0uuBh7m5ZdZgKs.png)
I don’t think you understood my suggestion, here’s an example with your struct defintion:
# Define the struct - this will automatically provide a constructor that takes all fields
struct lineStruct2
buses_line::Vector{Int}
res_line::Float64
ind_line::Float64
imp_mag_line::Float64
imp_angle_line::Float64
p_line::Float64
q_line::Float64
state_line::String
end
# Define an outer constructor that only requires the first three inputs, using default values for the remaining inputs
lineStruct2(buses_line, res_line, ind_line) = lineStruct2(buses_line, res_line,
ind_line, 0.0, 0.0, 0.0, 0.0, "overloaded")
# Test that it works
lineStruct2([1,2,3], 0.0, 0.0)
use Parameters.jl
I tried with both mutable and without it
I just pressed enter in the REPL and the error did not show again why is that ?
You have to restart Julia.
Also typenames should be CapitalCase (by convention). So call it LineStruct
, not lineStruct
.
yes, it worked thank you …
now
when I type for example
L2=lineStruct2([1,2,3],10,10)
I get an error that L2 is undefined
Why ?!
That seems unlikely. It would help if you could give a MWE with code written in mono-spaced font
```
using backticks like the above
your code here
```
to help isolate the problem.
I would highy recommend you read through the relevant section of the manual.
As @DNF says you cannot re-define types within the same Julia session:
julia> struct MyType
a::Float64
end
julia> struct MyType
no_wait_I_changed_my_mind
end
ERROR: invalid redefinition of constant MyType
Stacktrace:
[1] top-level scope at REPL[2]:1
Therefore, once you’ve defined LineStruct
you can only change that after restarting Julia. That’s also why in my example it was called LineStruct2
, because LineStruct
was already defined in the session when I created the MWE.
Can you restart Julia and run the following:
struct LineStruct
buses_line::Vector{Int}
res_line::Float64
ind_line::Float64
imp_mag_line::Float64
imp_angle_line::Float64
p_line::Float64
q_line::Float64
state_line::String
end
LineStruct(buses_line, res_line, ind_line) = LineStruct(buses_line, res_line,
ind_line, 0.0, 0.0, 0.0, 0.0, "overloaded")
l2 = LineStruct2([1,2,3], 0.0, 0.0)
Note that as per @DNF’s suggestion I’ve capitalized LineStruct
(the type) here, but lowercased l2
(an instance of the type).
This should work and do what you want.
Thanks alot… it worked and I will check the link too now Thanks again