473,323 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

beginner's question concerning operator overloading.

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::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
}
Jun 27 '08 #1
5 1393
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&)
Jun 27 '08 #2

"Carter" <ca*********@gmail.comwrote in message
news:f8**********************************@t12g2000 prg.googlegroups.com...
>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


Jun 27 '08 #3
Carter wrote:
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.

--
Jim Langston
ta*******@rocketmail.com
Jun 27 '08 #4
On Apr 28, 9:20 am, Carter <carterch...@gmail.comwrote:
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::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

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

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

return this;
}

and so on...
Jun 27 '08 #5
Thanks for the help. That solved the problem.
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.