Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 893205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:11:00+00:00 2026-05-15T14:11:00+00:00

I am looking to overload, let’s say, the addition operator and have it add

  • 0

I am looking to overload, let’s say, the addition operator and have it add two objects of the same class. When I declare this “operator+” prototype function in the class declaration in the header file, I pass in both objects as parameters. I get a compiler error saying that “binary ‘operator +’ has too many parameters”. I was searching around for an answer online and found out that declaring an inline function just outside the class declaration in the header file compiled out. I’m wondering what I was doing wrong or if I’m missing something here. Here is the code I am using in the header file.

class Person
{
private:
    int age;
    double weight;
public:
    Person::Person();                           //default constructor       
    Person::~Person();                           //default desctructor
    Person operator+(Person r, Person i);
};

This compiles with the error I mentioned above. Below is the code that compiles fine.

class Person
{
private:
    int age;
    double weight;
public:
    Person::Person();                           //default constructor       
    Person::~Person();                           //default desctructor
};
inline Person operator+(Person r, Person i)
{
return Person(0,0); 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:11:01+00:00Added an answer on May 15, 2026 at 2:11 pm

    If you declare oparator+ as an instance function then the first argument is being passed as this object and thus you need only one more argument. Read this for more info, in particular try to understand the const concept:

    http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html

    The best approach as advised in the referenced artice is:

    class Person
    {
        ...
        Person &operator+=(const Person &i);
        const Person operator+(const Person &i) const;
        ...
    };
    
    Person &Person::operator+=(const Person &i) {
        ...   // Do the compound assignment work.
        return *this;
    }
    
    const Person Person::operator+(const Person &i) const {    
        Person result = *this;     // Make a copy of myself.  Same as MyClass result(*this);
        result += i;            // Use += to add other to the copy.
        return result;              // All done!
    }
    

    if you decide to use const version remember that you will only be able to call const methods on this and i references. This is the preferred way.

    The article I referenced to explains the idea of overloading += first and then defining + using += in more details. This is a good idea since += operator must be separately overloaded.

    Additionally – David Rodríguez suggests operator+ to be implemented as a free function regardless of the presence of += .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class with a Uri property. Is there any way
Looking for C# class which wraps calls to do the following: read and write
Looking at the processmodel element in the Web.Config there are two attributes. maxWorkerThreads=25 maxIoThreads=25
Looking for suggestions on file system management tools. We have several terabytes of images,
Throughout our site there's a common model, let's say Video, that has a number
Looking at System.Collections.Generic.Dictionary<TKey, TValue> , it clearly implements ICollection<KeyValuePair<TKey, TValue>> , but doesn't have
I am looking up methods of the System.Data.Objects.DataClasses.StructuralObject in Reflector and I see that
Looking at posts like this and others, it seems that the correct way to
Looking for feedback on : http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools
Looking for an example that: Launches an EXE Waits for the EXE to finish.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.