A Problem about istream ">>" overloading

Discussion in 'C++' started by Colonel, Sep 29, 2007.

  1. Colonel

    Colonel Guest

    It seems that the problems have something to do with the overloading of
    istream operator ">>", but I just can't find the exact problem.

    // the declaration
    friend std::istream & operator>> (std::istream & in, const Complex & a);

    // the methods correspond to the friend
    std::istream & operator>> (std::istream & in, const Complex & a)
    {
    std::cout << "real: ";
    in >> a.real;
    std::cout << "imaginary: ";
    in >> a.imaginary;
    return in;
    }

    When complex0.cpp was compiled, such problem appeared:

    Compiling...
    complex0.cpp
    D:\C++\complex\complex0.cpp(45) : error C2679: binary '>>' : no operator
    defined which takes a right-hand operand of type 'const double' (or there is
    no acceptable conversion)
    D:\C++\complex\complex0.cpp(47) : error C2679: binary '>>' : no operator
    defined which takes a right-hand operand of type 'const double' (or there is
    no acceptable conversion)
    Error executing cl.exe.

    complex0.obj - 2 error(s), 0 warning(s)

    IDE: VC++ 6.0
    Can anybody point out the errors with the ">>" overloading?

    The overall code is as follows:

    // complex0.h -- definition of class Complex
    // used for complex operation
    #ifndef COMPLEX0_H_
    #define COMPLEX0_H_
    #include <iostream>

    class Complex
    {
    private:
    double real;
    double imaginary;
    public:
    Complex (); // default constructor
    Complex (double r, double i);
    ~Complex (); // destructor

    Complex operator- (const Complex & a) const;
    Complex operator- () const;
    Complex operator~ () const;

    friend std::istream & operator>> (std::istream & in, const Complex &
    a);
    friend Complex operator+ (const Complex & a, const Complex & b);
    friend Complex operator* (const Complex & a, const Complex & b);
    friend std::eek:stream & operator<< (std::eek:stream & os, const Complex &
    a);
    };
    #endif

    // complex0.cpp -- methods for class Complex
    // compiled with complex0.h
    #include "complex0.h" // constructors
    Complex::Complex ()
    {
    real = imaginary = 0.0;
    }
    Complex::Complex (double r, double i)
    {
    real = r;
    imaginary = i;
    }
    Complex::~Complex () // destructors
    {
    }

    // operators overloading

    Complex Complex::eek:perator - (const Complex & a) const // substract
    Complex a
    {
    return Complex (real - a.real, imaginary - a.imaginary);
    }

    Complex Complex::eek:perator - () const // reverse sign
    of Complex
    {
    return Complex (-real, -imaginary);
    }
    Complex Complex::eek:perator ~ () const // conjugate
    sign of Complex
    {
    return Complex (real, -imaginary);
    }

    // friends methods
    std::istream & operator>> (std::istream & in, const Complex & a)// input
    Complex
    {
    std::cout << "real: ";
    in >> a.real;
    std::cout << "imaginary: ";
    in >> a.imaginary;
    return in;
    }
    Complex operator+ (const Complex & a, const Complex & b) // plus two
    Complex
    {
    return Complex (a.real + b.real , a.imaginary + b.imaginary);
    }

    Complex operator* (const Complex & a, const Complex & b) // mutiple
    Complex a and b
    {
    return Complex (a.real * b.real , a.imaginary * b.imaginary);
    }

    std::eek:stream & operator<< (std::eek:stream & os, const Complex & a) //
    display Complex
    {
    os << "(" << a.real << ", " << a.imaginary << ")";
    return os;
    }
     
    Colonel, Sep 29, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,413
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    451
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    431
    Steve Holden
    Sep 30, 2005
  4. Colonel
    Replies:
    2
    Views:
    574
    Jim Langston
    Sep 29, 2007
  5. xmllmx
    Replies:
    5
    Views:
    592
    Jorgen Grahn
    Jun 15, 2010
Loading...

Share This Page