0

I'm trying to achieve this using parameter overloading using C++:

Complex c(3.0, 4.0);
double magnitude = | c; // magnitude will be 5

I wrote the following code: (Only the necessary part here..)

class Complex
{
   public:
       double _real;
       double _imaginary;

       friend double operator|(const Complex &c1)
       {
           return sqrt(c1._real * c1._real + c1._imaginary * c1._imaginary);
       }
}

But I get the following error:

error C2805: binary 'operator |' has too few parameters

Is that impossible to use operator | with only 1 parameter?

Sait
  • 19,045
  • 18
  • 72
  • 99

5 Answers5

7
friend double operator|(const Complex &c1)
{
    return sqrt(c1._real * c1._real + c1._imaginary * c1._imaginary);
}

This doesn't define a member operator, just FYI.

double magnitude = | c;

this is invalid syntax, | is a binary operator.

Correct way:

class Complex
{
   public:
       double _real;
       double _imaginary;

       double getMagnitude() const // POP POP!
       {
           return sqrt(_real * _real + _imaginary * _imaginary);
       }
}

No more bonus.

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Thanks for your answer. I have one more question: What is that `const` stands for after the `getMagnitude()` function? Does that mean "This function cannot change anything in the class" ? – Sait May 29 '12 at 10:21
  • @zagy yes, it can't change non-`mutable` members. It also can only call only other const methods. – Luchian Grigore May 29 '12 at 10:45
  • Downvote was a mistake, sorry. I agree that magnitude should be a property, not a side effect of an operator. If you edit your answer, I can clear the mistake. – nurettin May 29 '12 at 11:22
5

Is that impossible to use operator | with only 1 parameter?

You can overload operators as long as atleast one of the types involved is an user defined type but you cannot change the behavior w.r.t how many parameters they can take.
As the error message tells you | is a binary operator you cannot overload it to act as an unary operator.

What is the correct way to do this?

You should provide a utility function for your class Complex, do name it appropriately and it will to do the job for you in the best possible way.

Note that the very basic rule of operator overloading is:
"Whenever the meaning of an operator is not obviously clear and undisputed, it should not be overloaded. Instead, provide a function with a well-chosen name."
The rule is meant for non-intuitive operator usage like this.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 2
    Would be nice to know the reason for downvote. The very basic rule of operator overloading is *[Whenever the meaning of an operator is not obviously clear and undisputed, it should not be overloaded. Instead, provide a function with a well-chosen name.](http://stackoverflow.com/a/4421708/452307)* And the rule is meant for non-intuitive operators usage like this. – Alok Save May 29 '12 at 09:54
  • 1
    Probably one of the answerers, I got downvoted for no reason as well. +1 – Luchian Grigore May 29 '12 at 09:55
0

operator| is a binary operator. As binary operator, it needs 2 parameters. If you want to do what you want to do here, you must use an unary operator.

Anyway - it looks like a bad idea since it is not obvious from looking at the operator to see what it does.

Tobias Langner
  • 10,634
  • 6
  • 46
  • 76
0

No - the '|' operator is a binary operator, meaning that it takes two parameters. You can overload operators but not change their "arity". Some operators are available with multiple arities though.

Unary operators include:

  • +
  • -
  • ++ (both pre and post versions)
  • -- (both pre and post versions)
  • !
  • ~
  • *
  • &
  • (cast) (but you would have to define a suitable casting type to get your double result)

The best solution from a software engineering point of view would probably be an explicit method to get the modulus - e.g. getModulus(). But you could legitimately argue that a double cast is OK.

For the latter case, you would have:

class Complex
{
   public:
       double _real;
       double _imaginary;

       operator double() const
       {
           return sqrt(this._real * this._real + this._imaginary * this._imaginary);
       }
}

and use it as follows:

Complex c(3.0, 4.0);
double magnitude = c; // magnitude will be 5
CodeButcher
  • 564
  • 5
  • 16
-1

Is that impossible to use operator | with only 1 parameter?

Yes. operator| is a binary operator. That means it takes two arguments. What you're looking for is operator|=

struct wtf
{
    double operator|= (double omg)
    {  
        return 42.;
    }
};

int main(){ wtf omg; omg|=  42.; }
nurettin
  • 11,090
  • 5
  • 65
  • 85
  • -1 Please don't. You should never do unexpected things with operators. `|` is binary or in C++ and that's it. Not a magnitude operator for complex numbers. – RedX May 29 '12 at 10:01
  • I don't overload every operator I see. That's what the person seems to be asking. You have no reason to lecture me. If you want, you can go to boost.spirit forums and tell people that operator overloading should not be used in generation of DSLs. – nurettin May 29 '12 at 10:29