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

EDIT: See end of my post for working code, obtained from zeekay here .
See edit at the end of the question I'm trying to get a simple
EDIT: See this in action here: http://jsbin.com/emobi/5 -- and that's using mouseenter/mouseleave. I have
I have a Controller with two Edit methods (see below). When I submit the
See updated input and output data at Edit-1. What I am trying to accomplish
EDIT: You can see the search box live here . Before I explain, let
Intro: EDIT: See solution at the bottom of this question (c++) I have a
PLEASE SEE THE EDIT BELOW, THE REPORT SEEMS TO USE CACHED DATA? I cant
EDIT: I see why it doesn't run twice. I have the Ajax.BeginForm() call inside
[EDIT: Problem solved. Please see my answer below.] In my app I call the

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.