![]() |
Problem overloading functions
Alright, I've got this program that is supposed to ask the user for input of two ordered pairs, then use a class and a + operator to produce the midpoint. Unfortunately, whenever it is run, it gets the ordered pairs and then invariably says the midpoint is
(4370432,4370432). Here is the code: Code:
//Overloading Operators |
Re: Problem overloading functions
> a + b = c;
Try c = a + b; |
Re: Problem overloading functions
That works! But why should the order have any bearing on what the result is? Is it because I overloaded the + operator, but c++ has a default = operator?
|
Re: Problem overloading functions
Does a + b = c work for you for when you use say doubles? I doubt it.
lvalue = rvalue_expression is written into the language. You can't just rearrange that to suit. |
Re: Problem overloading functions
Let us consider the stmt
c= a + b In operator overloading concept, value next to + operator i.e operand value' b' only passed to Overloading function that why in ur pgm 'b' value is copied to' param' operand...... operand ' a' value is passed automatically ,we no need to pass it........ As per ur stmt, if we put a+b=c, Which value get passed? Think over it? |
Re: Problem overloading functions
Alright, I've got another problem. In the following code, I want to set m to equal c/2 (to finish off the midpoint formula). Why is this code incorrect?
Code:
CMidpoint CMidpoint::operator / ( CMidpoint otherparam )Code:
m = 2 / c; |
Re: Problem overloading functions
Actually, the second code block should say
Code:
m = c / 2; |
Re: Problem overloading functions
I think you also need a CMidpoint::operator / which takes an int as a parameter, and you use that parameter in your division.
|
Re: Problem overloading functions
if u use c=a/b(for overloading /) means i.e correct
but i think that m=c/2 or m=2/c for overloading is wrong..... |
Re: Problem overloading functions
I've got another question. I'm not totally grasping this overloading operators concept, and the book I'm trying to (partially) learn from isn't helping matters. It gives me the following program:
Code:
class VectorThanks alot. |
| All times are GMT +5.5. The time now is 01:47. |