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

The Archive Base Latest Questions

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

This may be a novice question, but I can’t figure it out by inspecting

  • 0

This may be a novice question, but I can’t figure it out by inspecting the book I have.
The class’s constructor initializes two doubles, and I want the following code to output those two doubles with <<.

Complex x( 3.3, 1.1 );

cout << "x: " << x;

After this I need to overload >> to accept two doubles into these.
This is my first question here, so if my information provided is lacking inform me

EDIT:
I now have for the constructor and overloading statement this:

#include "Complex.h"

Complex::Complex( double realPart, double imaginaryPart )
: real( realPart ),
imaginary( imaginaryPart )
{

}

std::istream& operator>>(std::istream& strm, const Complex &c)
{
   double r,i;
   strm >> r >> i;
   c = Complex(r,i);
   return strm;
}

I know I have to change the “const Complex &c” and the “c = Complex(r,i);” but I’m not sure how to go about it.
Also, I will say here that this is not about the std library’s Complex class, although it is based on the same idea. So far everyone has been a great help, but I have a case of the dumb today.

  • 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-12T23:15:28+00:00Added an answer on May 12, 2026 at 11:15 pm

    operator<< :

    std::cout is an std::ostream object, so you have to overload operator<< for ostream, which takes std::complex<double> as an argument, assuming you use std::complex<double> from the standard header complex. Since you shouldn’t make internal changes to standard containers and classes, make it standalone.

    #include <iostream>
    #include <complex>
    
    std::ostream& operator<<(std::ostream& strm, const std::complex<double>& c)
    {
        strm << "real: " << c.real() << "imag: " << c.imag();
        return strm;
    }
    

    operator>> :

    operator>> takes a std::istream object, which does the opposite of what std::ostream does. If you use streams for serialization like this, it’s a good idea to enable exceptions for them too. Usually you only want to throw on std::ios::badbit.

    #include <iostream>
    #include <complex>
    
    std::istream& operator>>(std::istream& strm, std::complex<double>& c)
    {
        double r,i;
        strm >> r >> i;
        c = std::complex<double>(r,i);
        return strm;
    }
    

    If you needed access to internal members of the class, you would define the overloaded function as a friend. But since std::complex::real() and std::complex::imag() are a part of the public interface, that’s not needed here. And for the istream example, we simply invoke the copy-constructor which is also a part of the public interface.

    I assumed you wanted to use cin and cout here. But if you wanted to overload the operators for something else, the same applies. If you implement the operators inside a class definition, you have access to the this pointer, hence the operator function should only take one argument.

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

Sidebar

Related Questions

This may be a stupid question but I have a code with the following
This may be a simple question but I can;t find the answer anywhere. Here
This may sound like a very generic question but here it goes. I have
This question may seem like a novice, and perhaps 'stupid' question but please bear
This may be have a better name than custom tab completion, but here's the
this may sound pretty straight forward, but still I want to post this question
This may seem a bit crazy, but if you can tell me a better
This may be a beginners question but not having much luck getting this going.
This may be a rather novice or even wrong question so please be forgiving.
This may be a dumb question, but that fuzzy blue color that you get

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.