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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:39:03+00:00 2026-05-22T18:39:03+00:00

I’ve been writing a complex numbers header from scratch for a school class but

  • 0

I’ve been writing a complex numbers header from scratch for a school class but I’m stuck on the extraction and insertion operators overload, I’ve been reading a lot about that topic but I still don’t get it

friend ostream& operator << (ostream &tmp, Cmplx &param)
{
    tmp<<param.Re<<"+"<<param.Im<<"i";
    return tmp;
}
friend istream& operator >> (istream &tmp, Cmplx &param)
{
    tmp>>param.Re;
    tmp>>param.Im;
    return tmp;
}

But when I try to compile I get.

no match for 'operator<<' in 'std::cout << Cmplx<vartype>::operator+(Cmplx<vartype>) [with vartype = long double](Cmplx<long double>(((const Cmplx<long double>&)((const Cmplx<long double>*)(& B)))))'

Thanks in advance

EDIT:
The Implementation:

#include"cmplx oper.hpp"
using namespace std;

int main()
{
Cmplx<long double> A, B;
cin >> A;
cin >> B;
cout<<(A+B)<<(A-B)<<(A*B)<<(A/B)<<(A+B).norm<<(A+B).pol<<(A+B).conj<<(A+B).re<<(A+B).im<<endl;
getch();
return true;
}

Also the modification, I changed the parameter to const:

friend ostream& operator << (ostream &tmp, Cmplx const &param)
{
    tmp<<param.Re<<"+"<<param.Im<<"i";
    return tmp;
}

Still not working

EDIT 2:
I broke down the cout line and found that the problem was a pair of methods in my class, not actually the use of the ‘+’ operator.
I still don’t know why but at least I could compile.

Also, I wonder if I can get an specific style input for my class, I mean something like

scanf("%d+%di",Re,Im);

But using cin (I cannot, or at least I don’t know how to use scanf since It’s a template, and it’s pretty awkward to write an specific cin for each type of data)

EDIT 3:
I found the problem, a lack of parenthesis.

  • 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-22T18:39:04+00:00Added an answer on May 22, 2026 at 6:39 pm

    You didn’t show your use of it, but in this case I can see what’s up.

    You’re doing something like std::cout << (Cmplx1 + Cmplx2);.

    The result of (Cmplx1 + Cmplx2) is a temporary; temporary expressions may not be bound to references.

    For example:

    int f() {
       return 3;
    }
    
    int& x = f(); // ill-formed
    

    However, as a special piece of C++ magic, temporaries can be bound to references-to-const:

    For example:

    int f() {
       return 3;
    }
    
    int const& x = f(); // magic!
    

    The temporary then lives as long as the reference-to-const does.

    If your operators take references to const complex objects, then you can bind a temporary as the second parameter:

    friend ostream& operator<<(ostream& os, Cmplx const& param)
    {
        os << param.Re << "+" << param.Im << "i";
        return os;
    }
    

    Conveniently, you ought to have done this in the first place as you will not be modifying param (and, in an operator<<, never should).

    Hope that helps.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but
I need to clean up various Word 'smart' characters in user input, including but
I'm looking for suggestions for debugging... If you view this site in Firefox or
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.