# Could not find an overload for "*"

I’m trying to follow the code in the book but am getting stuck. It says can not find overloads and unresolved identifier gravity.

//: Playground - noun: a place where people can play

import Cocoa

struct Vector {
var x: Double
var y: Double

``````init() {
self.init(x: 0, y:0)
}

init(x: Double, y: Double) {
self.x = x
self.y = y
}

func vectorByAddingVector(vector: Vector) -> Vector {
return Vector(x: x + vector.x, y: y + vector.y)
}
``````

}

class Particle {

``````var position: Vector
var velocity: Vector
var acceleration: Vector

init(position: Vector) {
self.position = position
self.velocity = Vector()
self.acceleration = Vector()
}

convenience init() {
self.init(position: Vector())
}

func tick(dt: NSTimeInterval) {
velocity = velocity + acceleration * dt
position = position + velocity * dt
position.y = max(0, position.y)
}
``````

}

class Simulation {
var particles: [Particle] =
var time: NSTimeInterval = 0.0

``````func addParticle(particle: Particle) {
particles.append(particle)
}

func tick(dt: NSTimeInterval) {
for particle in particles {
particle.acceleration = particle.acceleration + gravity
particle.tick(dt)
particle.acceleration = Vector()
}
time += dt
particles = particles.filter { particle in
let live = particle.position.y > 0.0
if !live {
println("Particle terminated at \(self.time)")
}
return live
}
}
``````

}

let simulation = Simulation()
let ball = Particle()
ball.acceleration = Vector(x: 0, y: 100)

func +(left: Vector, right: Vector) -> Vector {
}

func *(left: Vector, right: Double) -> Vector {
return Vector(x: left.x * right, y: left.y * right)
}

while simulation.particles.count > 0 && simulation.time < 500 {
simulation.tick(1.0)
}

let gravity = Vector(x: 0.0, y: -9.8)
let twoGs = gravity + gravity
let twoGsAlso = gravity * 2.0

Which version of XCode are you using? When I pasted your code into a XCode 11.5 playground I had a bunch of errors but none of them matched the ones you reported.

Keep in mind that this book is 5 years old, and Swift has changed significantly in that time. The book’s code won’t work in the current version of XCode without some tinkering. This thread has code for Chapter 3 that appears to be fairly up to date for the current version of Swift.

I’m using Xcode 6.3.