beginner's question concerning operator overloading.

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::eek:stream& operator << ( std::eek: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
}
 
C

Carter

Oops forgot to post this information sorry. Here is the error message
coming out of GCC-

vector.cpp: In function ‘int main()’:
vector.cpp:92: error: no match for ‘operator*’ in ‘2.0e+0 *
vector_3d(1.0e+0, 1.0e+0, 1.0e+0)’
vector.cpp:51: note: candidates are: vector_3d& operator*(vector_3d&,
double)
vector.cpp:56: note: vector_3d& operator*(double,
vector_3d&)
 
K

kwikius

Carter said:
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.

With a A quick look I think your problems due to const non const references.
At the error message you have created a temporary vector and the compiler
will not allow it to be passed into a function to as a non-const reference.
Of course changing the sig of your op functions will cause other problems.
However generally IMO where op is some non assignment operator (e.g + , * ,
/) you should return a copy of the vector rather than the original, and
pass your arguments by const reference rather than reference

V operator Op ( V const & x , V const & x);

rather than

V& operator Op ( V & x , V & x);

For multiply assign you can use *= etc.

This then mimics more closely the way the inbuilt types work and causes less
surprises.

regards
Andy Little
 
J

Jim Langston

Carter said:
Oops forgot to post this information sorry. Here is the error message
coming out of GCC-

vector.cpp: In function ‘int main()’:
vector.cpp:92: error: no match for ‘operator*’ in ‘2.0e+0 *
vector_3d(1.0e+0, 1.0e+0, 1.0e+0)’
vector.cpp:51: note: candidates are: vector_3d& operator*(vector_3d&,
double)
vector.cpp:56: note: vector_3d& operator*(double,
vector_3d&)

I may be wrong, but I belive because of const correctness.

inline vector_3d& operator*(double s, vector_3d & v)

This isn't matching
2.0 * vector_3d(1.0,1.0,1.0);

because vector_3d( 1.0, 1.0, 1.0 )
is a temporary.and you can't take a non constant reference to a temporary.

Changing the signature to
inline vector_3d& operator*(double s, const vector_3d & v)

should fix the problem.
 
T

Travis

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::eek:stream& operator << ( std::eek: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

}

Make your operator's methods of the class and correct the constness.
So you'd have...

vector_3d& vector_3d::eek:perator*(const vector_3d &rhs)
{
this->_x *= rhs._x;
this->_y *= rhs._y;
this->_z *= rhs._z;

return this;
}

and so on...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top