How Can Operator Overloading for Multiple Arguments Be Fixed in C++?

In summary, the conversation discusses an issue with an overloaded operator + in a newtype data type. The problem arises when trying to use the operator with more than two arguments, leading to a compiler error. The solution is to use const references as arguments in the operator+ function. The conversation ends with the acknowledgement that const is a necessary component in C++ programming.
  • #1
waht
1,501
4
I've got an overloaded operator +

Code:
newtype X, Y, Z, W;W = X + Y;  // works fine

W = Y + X; // works fine

// but with more arguments I'm getting compiler error

// saying there is no match for operator+

W = X + Y + Z;

Sample code
Code:
newtype operator+(newtype &A, newtype &B)
{

newtype C;

//more code

return C;

}

Is there something missing?
 
Technology news on Phys.org
  • #2
Just eyeballing it, I agree with the compiler.

Temporary objects cannot be implicitly converted to modifiable references -- the standard doesn't allow it, and even if it did, you shouldn't do it anyways because it can lead to all sorts of really, really hard to debug problems.

Since X+Y is a temporary value, it cannot be converted into the modifiable reference required by your operator+.


Now, the fix is actually very easy: write operator+ correctly! The arguments almost certainly should not be modifiable references -- they should be const references.

(Well, technically there are occasions where you're better off without using references at all)
 
Last edited:
  • #3
Not sure what you are doing but my code compilies fine. I am using 2008 C++ Express edition.

Here is my code.

Code:
#include<cmath>
#include<iomanip>
#include<iostream>

int x,y,z,w,pause;  // needed variables

using namespace std;

int main()
{
	x = 1;
	y = 2;
	z = 3;

	w = x+y;
	w = y+x;
	w = x+y+z;

	cout << w << endl;
	cin >> pause; // dummy input to keep the output screen visible


	return 0;
}

Thanks
Matt
 
  • #4
Even if "your code compiles fine" does not mean it is right. Besides, per your first post, your code does not compile fine. It doesn't even compile, period. I would not call that "fine".

Make the arguments to newtype operator+ be const references and then it will be fine.
 
  • #5
Yes, const is a must in C++.

Thanks
Matt
 
  • #6
Woe is me. Const ref works like a charm.
 

Related to How Can Operator Overloading for Multiple Arguments Be Fixed in C++?

1. What is operator overloading in C++?

Operator overloading in C++ is the ability to redefine or extend the behavior of existing operators to work with objects of user-defined classes. This allows for more intuitive and natural use of operators with custom data types.

2. How is operator overloading implemented in C++?

Operator overloading is implemented by overloading a function with the same name as the operator, using the keyword "operator" followed by the operator symbol. This function must be a member function of the class or a global function with at least one operand of the class type.

3. What operators can be overloaded in C++?

Most of the operators in C++ can be overloaded, except for the following: <, >, &&, ||, sizeof, and ?:

  • Unary operators: +, -, *, /, %, ++, --, !, ~
  • Binary operators: +, -, *, /, %, ==, !=, <, >, <=, >=, +=, -=, *=, /=, %=, &&, ||, &, |, ^, <<, >>
  • Assignment operators: =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=

4. What are the benefits of using operator overloading in C++?

Operator overloading can make code more readable and intuitive, as it allows for the use of familiar operators with custom data types. It can also improve code efficiency by reducing the amount of code needed for certain operations. Additionally, it can make code more maintainable by providing a logical and consistent way to perform operations on user-defined types.

5. What are the potential drawbacks of using operator overloading in C++?

One potential drawback is that it can lead to confusion or unexpected behavior if not used carefully. Overloading an operator may also change its original meaning, which can be confusing for other developers. Additionally, overloading too many operators can make the code difficult to understand and maintain. It is important to use operator overloading sparingly and only when it makes sense for the specific class and its intended use.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
53
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
5
Views
6K
Back
Top