"cannot accept the address" and "cannot call the pointer method to"

This compiles and works:

diff := projected.Minus(c.Origin)
dir := diff.Normalize()

This does not give (gives errors in the header):

dir := projected.Minus(c.Origin).Normalize()

Can someone help me understand why? (Go training)

These methods are:

// Minus subtracts another vector from this one
func (a *Vector3) Minus(b Vector3) Vector3 {
    return Vector3{a.X - b.X, a.Y - b.Y, a.Z - b.Z}
}

// Normalize makes the vector of length 1
func (a *Vector3) Normalize() Vector3 {
    d := a.Length()
    return Vector3{a.X / d, a.Y / d, a.Z / d}
}
+4
source share
1 answer

The method Vector3.Normalize()has a pointer receiver, so calling this method requires a pointer to the value Vector3( *Vector3). In the first example, you store the return value Vector3.Minus()in a variable that will be of type Vector3.

Go , diff.Normalize(), , diff, *Vector3, Normalize(). ""

(&diff).Normalize()

Spec: Calls:

x.m() , () x m, m. x &x m, x.m() (&x).m().

, , , , , Vector3.Minus().

, , Spec: :

, , , ; ; . x [ &x] (, ) .

. :

?

Go?

" "

" " ( ) - . .

- , ( ), , " ". , , , , ( , , ), , ).

- (*Vector3) Vector3. , , , .

, . :

func pv(v Vector3) *Vector3 {
    return &v
}

:

dir := pv(projected.Minus(c.Origin)).Normalize()

Vector3, :

func (v Vector3) pv() *Vector3 {
    return &v
}

:

dir := projected.Minus(c.Origin).pv().Normalize()

:

3 float64 , . . , . , .

+8

Source: https://habr.com/ru/post/1679246/


All Articles