1

Trying to do a subtraction of two vectors. In the end it should work like:

vector1.sub(vector2);

Custom Variable Vektor was defined as: Vektor(double x, double y, double z). Now I want to access the x, y, z coordinates through input.x, etc. Tells me

conversion from 'Vektor*' to non scalar type 'Vektor' requested.

Why tough??? Is it not possible to subtract a reference on to a value from a value?

Btw im new to SO so feel free to roast me for whatever ive done wrong!;)

Vektor Vektor::sub(const Vektor& input) const
{
    Vektor subresult = new Vektor(x - input.x, y - input.y, z - input.z);
    return subresult;
}
YSC
  • 38,212
  • 9
  • 96
  • 149

1 Answers1

5

You should not use new here, just return by value

Vektor Vektor::sub(const Vektor& input) const
{
    return Vektor(x - input.x, y - input.y, z - input.z);
}

Also note that you can override operator- so you can perform a subtraction using the syntax v1 - v2 where each are of type Vektor.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218