How translate C++ To Swift

I have little knowledge of C ++, I tried to declare the operator as follows:


struct Simple2D {

var x: Double! , y: Double!

func setS(x0:Double , y0:Double)->Simple2D {

return Simple2D(x: x0, y: y0)

}

func lenght(v: Simple2D)->Double {

return pythagorous2(v.x, b:v.y)

}

}

var tx = Simple2D(x: 2.0, y: 2.0)

prefix operator * {}

prefix func * (v: Simple2D)-> Double {

return tx.x*v.x + tx.y*v.y

}

this does not work, how do I call the functions from another file?


This original file in c++ :

ile Simple2D.h

template <typename T>

struct Simple2D

{

T x, y;

/

Simple2D <T>() {

x = y = 0;

}

/

Simple2D <T>(const T x0, const T y0) : x(x0), y(y0) {

}

/

void operator ()(const T x0, const T y0)

{

x= x0; y= y0;

}

/

bool operator ==(const Simple2D<T> &v) const;

/

Simple2D <T> operator -(const Simple2D <T> &v) const;

/

Simple2D <T> operator -(void) const;

}

file Simple2D.cpp

template <typename T>

bool Simple2D <T>::operator ==(const Simple2D <T> &v) const

{

return (is_equal(x,v.x) && is_equal(y,v.y));

}

template <typename T>

Simple2D <T> Simple2D <T>::operator -(void) const

{

return Simple2D <T>(-x,-y);

}

template <typename T>

Simple2D <T> Simple2D <T>::operator -(const Simple2D <T> &v) const

{

return Simple2D <T>(x-v.x, y-v.y);

}

Please wrap code in the code tags by selecting the code and then hitting the <> button.

OMG, is that how it's done!? I've been hitting the code button first, then struggling with pasting it in properly. Thanks for the pointer!

I'd write your file like so:



struct Simple2D: Equatable {
    let x: Double
    let y: Double
  
    init(x: Double, y: Double) {
        self.x = x
        self.y = y
    }
  
    init() {
        x = 0
        y = 0
    }
  
    func length() -> Double {
        return pythagorous2(x, b: y)
    }
}
func ==(lhs: Simple2D, rhs: Simple2D) -> Bool {
    return lhs.x == rhs.x && lhs.y == rhs.y
}
func *(lhs: Simple2D, rhs: Simple2D) -> Double {
    return lhs.x * rhs.x + lhs.y * rhs.y
}
func -(lhs: Simple2D, rhs: Simple2D) -> Simple2D {
    return Simple2D(x: lhs.x - rhs.x, y: lhs.y - rhs.y)
}
prefix func -(x: Simple2D) -> Simple2D {
    return Simple2D(x: -x.x, y: -x.y)
}

this does not work, how do I call the functions from another file?

Can you be more specific with "this does not work"? As far as I tested, with putting the (guessed) definition of pythagorous2 in another file, your code works.

thank you very much, You were very kind

How translate C&#43;&#43; To Swift
 
 
Q