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 324803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:09:28+00:00 2026-05-12T09:09:28+00:00

See edit at the end I am trying to overload the + operator in

  • 0

See edit at the end

I am trying to overload the + operator in C++ to allow me to add two complex numbers. (add the real and add the imaginary).

Here is my overloaded function:

ComplexNum operator+(ComplexNum x, ComplexNum y){
ComplexNum result;
result.real = (x.getReal() + y.getReal());
result.imag = (x.getImag() + y.getImag());

return result;
}

My Complex number constructor takes in two ints and assigned the first to int real and second to int imag.

When I try add them:

ComplexNum num1 = ComplexNum(1,1);
    ComplexNum num2 = ComplexNum(2,3);
    ComplexNum num3;
    num3 = num1 + num2;
    printf("%d",num3.getReal());

I get 0 as a result. The result should be 3 (the real parts of num1 and num2 added)

EDIT: I figured out what was wrong. I had .getReal() and .getImage() returning double.

  • 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-12T09:09:28+00:00Added an answer on May 12, 2026 at 9:09 am

    Since the arguments of operator+ are declared as values, not references, they will be passed by copy, so if the copy-constructor of ComplexNum doesn’t work, that could cause x and y to have a real part of 0.

    It’s also possible that the addition works, but you lose the real part when you invoke the assignment operator of num3.

    Or maybe it’s simply the getReal() method that is broken.

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

Sidebar

Related Questions

(see here for the problem I'm trying to solve) How do you get hibernate
EDIT: See my working code in the answers below. In brief: I have a
EDIT: I would really like to see some general discussion about the formats, their
Edit: I have solved this by myself. See my answer below I have set
I receive this message (see image below) when I try to edit in debugging.
Im trying to update some nested params from a form. I can see that
** New EDIT ** so what I'm trying to do is this. I want
Here's what I'm thinking: class Widget < ActiveRecord::Base has_one :widget_layout end class WidgetLayout <
See title. By lightweight markup I mean something like markdown or wikitext.
See title: what does it mean for a C++ function to be inline?

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.