0

When I did an exercise of class about complex number in C++ Primer Plus,the ideal effect is to input a complex number a+bi and output a complex number a+bi.

But I encountered a problem:the compilation error is so many that I can't list.

//This is the defination
1.istream& operator>>(istream& is, complex const&a) {                                     
                         is >> a.real>>"+">> a.imag>>"i";                                                           
                         return is;                                                                          
                         }

2.ostream& operator<<(ostream& os, complex const&a) {
        os << a.real << "+" << a.imag << "i";
        return os;
    }

//the relevant class:
class complex {
    private:
        double real;
        double imag;......
}

I want to know the difference of operator overloading between >> and<<, what's wrong with the code,and how to use>>to input const char like 'i'or'+'or other alternative method.

Implemention:I took some advice from you,and I'm sorry that I forget to add standard Header File in my "mycomplex.h"File. This is part of my modified code.

std::istream& operator>>(std::istream& is, complex &a) {//Aovid using "using namespace std".
        char x1='\0', x2='\0';//Using nonconst variable after ">>"
        is >> a.real >> x1;
        if (x1 == '-')//Consider a value with anegative imaginary part
            a.imag = -a.imag;
        is>> a.imag >> x2;
        if ((x1 != '+'&&x1!='-') || x2 != 'i')//Check if it's in a relevant form
            std::cout << "Argument error!";
        return is;
    }
    std::ostream& operator<<(std::ostream& os, const complex a) {
        return os << a.real << "+" << a.imag << "i";
    }

Finally,thanks for your contributions! :)

Victor Hut
  • 35
  • 5
  • 1
    Please try to prepare a [mcve] and include the compiler error message in the question. – 463035818_is_not_an_ai May 12 '22 at 13:38
  • 2
    does `cin >> "+";` normally work? – user253751 May 12 '22 at 13:39
  • 2
    `operator>>` reads from the stream and writes to the right operand. `is >> "+"` does not make sense. You can read a single `char` when you know that there is a single `char` to be skipped from input – 463035818_is_not_an_ai May 12 '22 at 13:39
  • 2
    You can't `>>` into a `const` object. Lose the "const" on that one. – molbdnilo May 12 '22 at 13:39
  • btw i guess you have some `using namespace std;` this alone is not nice ( [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)) but in combination with reusing names from `std` it can cause lots of confusion (note that there is `std::complex` and while unlikely you dont know what other header you do include directly includes ``) – 463035818_is_not_an_ai May 12 '22 at 13:41
  • Refer to [overloading-operators-c-complex-numbers](https://stackoverflow.com/questions/62377654/overloading-operators-c-complex-numbers) – Jason May 12 '22 at 13:48
  • [This](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) is worth reading, too. – Paul Sanders May 12 '22 at 13:56
  • When reading data that you expect to be of the form `a+bi` you need to *check* if input is in a relevant form (in this case, that the stream of characters contains (representation of) a floating point value followed by a `'+'` then another floating point value, then the letter `'i'`). More importantly, your input function needs to deal sanely with data that isn't quite in that form (e.g. if there is a missing value, if there is whitespace, etc). Your code does no such checking or correction of input. Also, consider what your code would write (or read) for a value with negative imaginary part. – Peter May 12 '22 at 14:13

0 Answers0