Sign in to follow this  

problem defining = operator

This topic is 3460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey. From the code it is easy to see what I'm trying to do. But this isn't working! I get the error: " C:\Users\Remy\Documents\CPP Enterprise game\geometry.h|43|error: no match for 'operator=' in '((Rect*)this)->Rect::start = (((Vector2D*)operator new(16u)), (<anonymous>->Vector2D::Vector2D(), <anonymous>))" Code: class Vector2D { public: double x; double y; public: Vector2D& operator= (const Vector2D& param) { x=param.x; y=param.y; return *this; } }; class Rect { public: Vector2D start; Vector2D end; Rect() {start = new Vector2D(); end = new Vector2D();} };

Share this post


Link to post
Share on other sites
C++ uses value semantics: the members are created when the object is created. The default constructor, as well as the default assignment operator, already do what you need. Therefore, you could simply write:


struct Vector2D
{
double x;
double y;
};

struct Rect
{
Vector2D start;
Vector2D end;
};


I changed "class Foo { public:" to "struct Foo {" because it's shorter.

Share this post


Link to post
Share on other sites

This topic is 3460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this