Jump to content
  • Advertisement
Sign in to follow this  
ninjackal

Error c2784 With + Operator Overload

This topic is 3917 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

First of all, here is my code: My VERTEX struct with the overloaded + operator... struct VERTEX { float x; float y; VERTEX operator+(VERTEX v) { v.x += x; v.y += y; return v; } }; And the error occurs with this line of code... v = v1 + v2; // Where v, v1 and v2 are declared as VERTEX The error I receive is this: error C2784: 'std::reverse_iterator<_RanIt> std::operator +(_Diff,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'VERTEX' I don't understand why I would get this error. Any help is much appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by ninjackal
v = v1 + v2; // Where v, v1 and v2 are declared as VERTEX


The compiler seems to disagree.

Also, you might want to add const-correctness to the mix.

Share this post


Link to post
Share on other sites
You are mixing 2 things here being operator+ and operator+=

operator+ has signature TYPE operator+(const TYPE&) const or
TYPE operator+(const TYPE&, const TYPE&).

whereas operator+= has signature const TYPE& operator+=(const TYPE&)

struct VERTEX
{
float x, y;

VERTEX operator+(const VERTEX& v)
{
VERTEX result;
result.x = this->x + v.x;
result.y = this->y + v.y;
return result;
}
};

or better:

struct VERTEX
{
VERTEX() {}
VERTEX(float _x, float _y) : x(_x), y(_y) {}
float x, y;
};

inline VERTEX operator+(const VERTEX &lhs, const VERTEX &rhs)
{
return VERTEX(lhs.x + rhs.x, lhs.y + rhs.y);
}


Whereas if you want to do += what the body of your code suggested then:

struct VERTEX
{
VERTEX() {}
VERTEX(float _x, float _y) : x(_x), y(_y) {}
const VERTEX& operator+=(const VERTEX&rhs){ x+=rhs.x; y+=rhs.y; return *this; }
float x, y;
};


Hope this helps

Share this post


Link to post
Share on other sites
Try this (no guarantees tough ;) ):


VERTEX operator+(const VERTEX& v) const
{
VERTEX tmp = *this;
tmp.x += v.x;
tmp.y += v.y;
return tmp;
}



Your mistake was basically that you don't allow const Vertices to be added to each other. Hence the added "const"'s in my code.
Secondly, I made the parameter a reference in order to create one temporary object less.

Hope that helps!

Share this post


Link to post
Share on other sites
Oops, my bad. I have two copies of the same file, one of which is being used by project, and the other isn't. I was editing the wrong file, what a waste of time. Sorry for the trouble.

My original code works but the suggestions I've received seem to be more efficient, so I guess it wasn't a complete waste. Thanks again for the help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!