You are here:

C++/operator+()

Advertisement


Question

hello

i'm trying to use the operator+() to overload the + operator
so it can add multiple objects, not just 2

I'm trying to send the operator+() an array of objects, but it shows me an error message (error "too many args to operator+()")

how can i send several objects so i can
add them like:

total = employee1 + employee2 + employee3 + employee4...employeeN

thanks

Answer
It would help to see your + operator code so I can see what you were trying to do.

But generally to do this kind of thing (multiple operations, cascaded ='s etc.), the operator must return a reference:

MyClass &operator( MyClass &operand )

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.