C++ issue with + operator
May 23, 2019 at 4:43pm UTC
My overload+ operator is not working properly. I am getting (0,0) for my output when it should have values instead. Would appreciate if anyone could help.
Correct output:
(4.2,12.6)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
class fvector2D: public std::complex<float >
{
public :
fvector2D(float x=0, float y=0) : std::complex<float >(x, y)
{
}
fvector2D(std::complex<float >) {} //conversion constructor
};
fvector2D operator +(const fvector2D& , const fvector2D&);
fvector2D operator +(const fvector2D& lhs, const fvector2D& rhs)
{
return static_cast <const std::complex<float > &>( lhs) + static_cast <const std::complex<float > &>(rhs);
}
int main()
{
fvector2D v(1, 4.1f);
fvector2D u(3.2f, 5.5f);
fvector2D w = u + v;
std::cout << w << std::endl;
}
Last edited on May 23, 2019 at 4:57pm UTC
May 23, 2019 at 4:48pm UTC
fvector2D::fvector2D(std::complex<float>) doesn't do anything with its parameter. You probably meant to do
fvector2D(const std::complex<float > &other): std::complex<float >(other){}
Topic archived. No new replies allowed.