When I try to compile my project I get a C2440 error saying 'initializing' : cannot convert from 'vector<component_count>' to 'vector_2D'.
The MSDN documentation on C2440 says The compiler cannot cast from 'type1' to 'type2'.
I have a base class, vector:
template <unsigned int component_count>
class vector {...}
and a derived class, vector_2D:
class vector_2D : public vector<2>
My base class defines a default and copy constructor, and operator overloads. The code which gives me the error is:
vector_2D character_position = pen_position + vector_2D(offset_x, offset_y);
pen_position is created earlier as
vector_2D pen_position(string_position);
I can't seem to fix my error. Can anyone point out what is going wrong and/or how to fix it?
vector operator + (const vector& a_vector) const
is the problem. You're returning a base and then trying to assign it to the derived.
The simplest fix is to provide an overload
vector_2D operator + (const vector_2D& a_vector) const
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.