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

  • SEARCH
  • Home
  • 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 6560671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:29:25+00:00 2026-05-25T13:29:25+00:00

I have a class named ThreeDigits on c++ code. I overloaded the + operand,

  • 0

I have a class named ThreeDigits on c++ code. I overloaded the + operand, this way:

ThreeDigits* ThreeDigits::operator+(const ThreeDigits &number) const

{
   double result= getNumber()+number.getNumber();
   ThreeDigits* new_result=new ThreeDigits(result);
   return new_result;
}

but when I write on the main function:

    ThreeDigits* first=new ThreeDigits(2.55998);
    ThreeDigits* second=new ThreeDigits(5.666542);
    ThreeDigits* result=first+second;

I get the following compilation error:
invalid operands of types ThreeDigits* and ThreeDigits* to binary operator+

Can you tell me what is the problem? thanks

  • 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-25T13:29:26+00:00Added an answer on May 25, 2026 at 1:29 pm

    You are trying to sum pointers to objects instead of the objects themselves. To invoke the overloaded operator you must call it on objects, thus dereferencing the pointers.

    By the way, creating all those objects with new a terrible way to do C++; in C++, unlike Java/C#, you should use new only when you have to, and allocate all the rest on the stack. Having the operator+ return a pointer to a newly created object is an abomination.

    The C++ way of writing your code would be:

    ThreeDigits ThreeDigits::operator+(const ThreeDigits &number) const
    {
       return ThreeDigits(getNumber()+number.getNumber()); // construct a temporary and return it
    }
    
    // ...
    
    ThreeDigits first(2.55998);
    ThreeDigits second(5.666542);
    ThreeDigits result=first+second;
    

    By the way, the usual way of overloading arithmetic operators is first overloading the assigning versions (+=, -=, …) and then build the “normal” version over them. For more info about operator overloading see the operator overloading FAQ.

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

Sidebar

Related Questions

I have a class named baseClass. From this class I inherit a class names
I have a class named Person and in this class is the property PersonName
I have a class named Graph, in this class I have a member named
I have a class named Entry declared like this: class Entry{ string Id {get;set;}
I have an class named Foo. This class contains a collection of child objects
I have a class named MyClass and I want this class to extend Graphics2D
I have a class named ExampleHandler extends from DefaultHandler . This class reads data
In a .NET windows app, I have a class named EmployeeManager. On instantiation, this
I have a class named Employee.This class have 3 attributes:name,adress and supervisor.
I have a class named Person .This class represents (as the name says) a

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.