C
Carter
I am getting a recurring error with my multiply operator for a vector
class I am working on and I am uncertain as to the exact reason why
the operator* fails to match properly in certain circumstances yet
seems to work properly in others. Any help would be appreciated.
Thanks in advance,
Carter.
(Code follows)
#include <iostream>
class vector_3d
{
public:
vector_3d()
:_x(0.0), _y(0.0), _z(0.0) {}
vector_3d(double x, double y, double z)
:_x(x), _y(y), _z(z) {}
vector_3d(const vector_3d& v)
:_x( v._x ), _y ( v._y ), _z( v._z ) {}
void set_x(double x) { _x = x; }
void set_y(double y) { _y = y; }
void set_z(double z) { _z = z; }
double x() const { return _x; }
double y() const { return _y; }
double z() const { return _z; }
private:
double _x, _y, _z;
};
vector_3d& operator*=(vector_3d & v, double s)
{
v.set_x( s * v.x() );
v.set_y( s * v.y() );
v.set_z( s * v.z() );
return v;
}
inline vector_3d& operator*(vector_3d & v, double s)
{
return (v *= s);
}
inline vector_3d& operator*(double s, vector_3d & v)
{
return (v *= s);
}
std:stream& operator << ( std:stream& os, const vector_3d& v)
{
return os << v.x() << "," << v.y() << "," << v.z() << std::endl;
}
int main()
{
// vector_3d v(1.0,1.0,1.0);
vector_3d v = 2.0 * vector_3d(1.0,1.0,1.0); // this errors out
std::cout << (2.0 * v); // this does not
}
class I am working on and I am uncertain as to the exact reason why
the operator* fails to match properly in certain circumstances yet
seems to work properly in others. Any help would be appreciated.
Thanks in advance,
Carter.
(Code follows)
#include <iostream>
class vector_3d
{
public:
vector_3d()
:_x(0.0), _y(0.0), _z(0.0) {}
vector_3d(double x, double y, double z)
:_x(x), _y(y), _z(z) {}
vector_3d(const vector_3d& v)
:_x( v._x ), _y ( v._y ), _z( v._z ) {}
void set_x(double x) { _x = x; }
void set_y(double y) { _y = y; }
void set_z(double z) { _z = z; }
double x() const { return _x; }
double y() const { return _y; }
double z() const { return _z; }
private:
double _x, _y, _z;
};
vector_3d& operator*=(vector_3d & v, double s)
{
v.set_x( s * v.x() );
v.set_y( s * v.y() );
v.set_z( s * v.z() );
return v;
}
inline vector_3d& operator*(vector_3d & v, double s)
{
return (v *= s);
}
inline vector_3d& operator*(double s, vector_3d & v)
{
return (v *= s);
}
std:stream& operator << ( std:stream& os, const vector_3d& v)
{
return os << v.x() << "," << v.y() << "," << v.z() << std::endl;
}
int main()
{
// vector_3d v(1.0,1.0,1.0);
vector_3d v = 2.0 * vector_3d(1.0,1.0,1.0); // this errors out
std::cout << (2.0 * v); // this does not
}