Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > beginner's ques...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 6 Topic 45613 of 47995
Post > Topic >>

beginner's question concerning operator overloading.

by Carter <cartercheng@[EMAIL PROTECTED] > Apr 28, 2008 at 09:20 AM

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 cir***stances 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::ostream& operator << ( std::ostream& 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
}
 




 6 Posts in Topic:
beginner's question concerning operator overloading.
Carter <cartercheng@[E  2008-04-28 09:20:34 
Re: beginner's question concerning operator overloading.
Carter <cartercheng@[E  2008-04-28 09:29:57 
Re: beginner's question concerning operator overloading.
"Jim Langston"   2008-04-28 10:35:06 
Re: beginner's question concerning operator overloading.
"kwikius" <a  2008-04-28 17:44:31 
Re: beginner's question concerning operator overloading.
Travis <travis.bowers@  2008-04-28 11:57:44 
Re: beginner's question concerning operator overloading.
Carter <cartercheng@[E  2008-04-29 07:31:03 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 21:25:59 CDT 2008.