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

  • Thread starter Thread starter waht
  • Start date Start date
  • Tags Tags
    C++ Operator
AI Thread Summary
The discussion revolves around issues with operator overloading in C++, specifically the implementation of a custom operator+ for a new type. The initial implementation encounters compiler errors when attempting to add more than two operands, due to the inability to convert temporary objects into modifiable references. The consensus is that the operator+ should take const references instead of modifiable references to avoid these errors. A participant shares a working example of code that compiles successfully, highlighting that using const references resolves the issue. The importance of adhering to C++ standards and best practices, such as using const references, is emphasized to prevent debugging difficulties.
waht
Messages
1,499
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top