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

  • Context: C/C++ 
  • Thread starter Thread starter waht
  • Start date Start date
  • Tags Tags
    C++ Operator
Click For Summary

Discussion Overview

The discussion revolves around the issue of operator overloading for multiple arguments in C++, specifically focusing on the operator +. Participants explore the reasons behind compiler errors when attempting to use the operator with more than two operands and discuss potential solutions.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an issue with operator + when trying to add three objects of a custom type, receiving a compiler error.
  • Another participant suggests that the error arises because temporary objects cannot be implicitly converted to modifiable references, recommending the use of const references instead.
  • A different participant claims their code compiles without issues, providing a simple example using primitive types, which does not directly address the custom type issue.
  • One participant challenges the claim of successful compilation, asserting that the original code does not compile and reiterates the need for const references in operator overloading.
  • Another participant agrees on the necessity of using const references in C++ for operator overloading.
  • A later reply confirms that using const references resolves the issue.

Areas of Agreement / Disagreement

There is disagreement regarding the compilation of the original code. While one participant asserts that their code works, others maintain that it does not compile correctly and emphasize the importance of using const references.

Contextual Notes

The discussion highlights the importance of understanding reference types in C++ operator overloading, particularly regarding temporary objects and modifiable references. There are unresolved aspects related to the specific implementation of the custom type and its operator overload.

Who May Find This Useful

Programmers working with C++ who are interested in operator overloading, particularly those encountering issues with temporary objects and reference types.

waht
Messages
1,502
Reaction score
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
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:
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
 
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.
 
Yes, const is a must in C++.

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

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
Replies
53
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
4
Views
5K
Replies
5
Views
6K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 39 ·
2
Replies
39
Views
5K