0

I have to implement for a given class Vector2D with the attributes float x and float y. Implement the operators + and <<.

class Vector2D
{
private:
float x;
float y;
public:
//No Constructor and Deconstructor needed!
}

my Implementation:

Vector2D operator+(Vector2D& vector1)
{
   Vector2D vTemp{0F,0F};
   vTemp.x = x + vector1.x;
   vTemp.y = y + vector1.y;
return vTemp;
}
std::ostream& operator<<(std::ostream& ost, Vector2D vector1)
{
   ost << vector1.x << " " << vector1.y << std::endl;
  return ost;
}

So i just wanted to know if my implementation is correct? Thank you

FooStack
  • 1
  • 1
  • 1
    Why do you think it may not be correct? Have you tested it, did you verify that you get the correct results? – Sam Varshavchik Jul 02 '22 at 18:02
  • Have you checked whether this code compiles without errors? I notice that I need to fix _a lot_ of mistakes just to get the code shown here to compile. – Drew Dormann Jul 02 '22 at 18:04
  • Besides the duplicate, you might also be interested in [this canonical implementation reference](https://en.cppreference.com/w/cpp/language/operators#Canonical_implementations). – Some programmer dude Jul 02 '22 at 18:04

0 Answers0