# My function for a struct complains access to undefined reference?

**URL:** <https://discourse.julialang.org/t/my-function-for-a-struct-complains-access-to-undefined-reference/64092>\
**Category:** New to Julia\
**Created:** [July 5, 2021, 5:40pm UTC](https://discourse.julialang.org/t/my-function-for-a-struct-complains-access-to-undefined-reference/64092 "2021-07-05T17:40:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![EMCP](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@EMCP](https://discourse.julialang.org/u/EMCP)\
**Post date:** [July 5, 2021, 5:40pm UTC](https://discourse.julialang.org/t/my-function-for-a-struct-complains-access-to-undefined-reference/64092/1 "2021-07-05T17:40:19Z")

</div>

I have 3 julialang files so far… and trying to get a working setup… but hit a snag … here’s my code

**dense\_layer.jl**

```julia
mutable struct LayerDense
    num_inputs
    num_neurons
    weights::Matrix{Float64}
    biases::Matrix{Float64}
    output::Matrix{Float64}

    LayerDense(num_inputs::Integer, num_neurons::Integer) = new(0.01 * randn(num_inputs, num_neurons), zeros((1, num_neurons)))
end

function forward(layer::LayerDense, inputs)
    layer.output = inputs * layer.weights + layer.weights
end

```

exercise.jl

```julia
include("dense_layer_exercise/spiral_data.jl")
include("dense_layer_exercise/dense_layer.jl")

coords, color = spiral_data(100, 3)

dense1 = LayerDense(2,3)

forward(dense1, coords)

using Plots

scatter(coords[:,1], coords[:,2], zcolor=color, framestyle=:box)

```

my error… I can see it is complaining that I have some… reference it cannot understand. I thought adding the `output::Matrix{Float64}` would fix it but no luck

```julia
ERROR: LoadError: UndefRefError: access to undefined reference
Stacktrace:
 [1] getproperty
   @ ./Base.jl:33 [inlined]
 [2] forward(layer::LayerDense, inputs::Matrix{Float64})
   @ Main ~/dense_layer_exercise/dense_layer.jl:15
 [3] top-level scope
   @ ~/exercise.jl:11
in expression starting at /exercise.jll:11

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [July 5, 2021, 5:46pm UTC](https://discourse.julialang.org/t/my-function-for-a-struct-complains-access-to-undefined-reference/64092/2 "2021-07-05T17:46:44Z")

</div>

In your `dense1` , `weights` and `biases` are undefined, because you didn’t initialize them in constructor.

---

<div class="post-metadata">

**Author:** ![EMCP](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@EMCP](https://discourse.julialang.org/u/EMCP)\
**Post date:** [July 5, 2021, 7:09pm UTC](https://discourse.julialang.org/t/my-function-for-a-struct-complains-access-to-undefined-reference/64092/3 "2021-07-05T19:09:32Z")

</div>

ah I see now…

I had thought I had assigned them via the constructor… but now realize I had referenced the number of neurons and inputs instead… but when I switch to this… it seems to fail as well

```julia
mutable struct LayerDense
...
    LayerDense(weights, biases) = new(0.01 * randn(num_inputs, num_neurons), zeros((1, num_neurons)))
....

```

I think I will read up on constructors… I had some advice from a previous question to get this far but it seems this is not quite 100%

```julia
mutable struct LayerDense
    inner_num_inputs
    inner_num_neurons
    weights::Matrix{Float64}
    biases::Matrix{Float64}
    output::Matrix{Float64}

    LayerDense(num_inputs::Integer, num_neurons::Integer)
        weights = new(0.01 * randn(num_inputs, num_neurons))
        biases = zeros((1, num_neurons))
        inner_num_inputs = num_inputs
        inner_num_neurons = num_neurons
end

function forward(layer::LayerDense, inputs)
    layer.output = inputs * layer.weights + layer.weights
end

```

seems im not grasping how to assign to structs variables in the constructor

edit:

okay I think I have gotten it… my constructor is basically going to go down the line of structs IN-ORDER… which I was not aware of but now seems obvious… so if I want to initialize and save the weights I must bump it up to the top like so

```julia

mutable struct LayerDense
    weights::Matrix{Float64}
    biases::Matrix{Float64}
    num_inputs::Integer
    num_neurons::Integer
    output::Matrix{Float64}
    LayerDense(num_inputs::Integer, num_neurons::Integer) = new(0.01 * randn(num_inputs, num_neurons), zeros((1, num_neurons)),num_inputs, num_neurons)
end

```
