Overloaded << Operator Error ?? why

Discussion in 'C++' started by pallavsingh81@gmail.com, Apr 10, 2008.

  1. Guest

    #include<iostream.h>

    //prefix and postfix Operator overloading // Inorder to see effect
    overload operator <<

    class A
    {
    public :
    int i,j;
    A(int _i,int _j):i(_i),j(_j){}
    A & operator++();
    const A operator++(int);

    };


    A& A::eek:perator++()
    {
    this->i = this->i + 1;
    this->j = this->j +1 ;
    return *this;
    }



    const A A::eek:perator++(int)
    {
    A obj = *this;
    this->i = this->i + 1;
    this->j = this->j +1 ;

    return obj;
    }

    ostream & operator << (ostream & out , A & obj)
    {
    out<<"Value of i "<<obj.i<<endl;
    out<<"Value of j "<<obj.j<<endl;
    return out;
    }

    int main()
    {

    A obj(10,20);
    cout<<++obj;
    cout<<obj++; // Error Why ????????????????

    return 0;
    }
    , Apr 10, 2008
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

    > #include<iostream.h>


    This is an obsolete non-standard header. You shouldn't use it.

    > //prefix and postfix Operator overloading // Inorder to see effect
    > overload operator <<
    >
    > class A
    > {
    > public :
    > int i,j;
    > A(int _i,int _j):i(_i),j(_j){}
    > A & operator++();
    > const A operator++(int);
    >
    > };
    >
    >
    > A& A::eek:perator++()
    > {
    > this->i = this->i + 1;
    > this->j = this->j +1 ;
    > return *this;
    > }
    >
    >
    >
    > const A A::eek:perator++(int)
    > {
    > A obj = *this;
    > this->i = this->i + 1;
    > this->j = this->j +1 ;
    >
    > return obj;
    > }
    >
    > ostream & operator << (ostream & out , A & obj)


    So this operator claims to modify A. Since it doesn't, the second parameter
    should be a reference to const A. This will also solve your problem.

    > {
    > out<<"Value of i "<<obj.i<<endl;
    > out<<"Value of j "<<obj.j<<endl;
    > return out;
    > }
    >
    > int main()
    > {
    >
    > A obj(10,20);
    > cout<<++obj;
    > cout<<obj++; // Error Why ????????????????


    Because the object returned by the operator is a temporary, which cannot be
    bound to a non-const reference.

    > return 0;
    > }
    Rolf Magnus, Apr 10, 2008
    #2
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,833
    Smokey Grindel
    Dec 2, 2006
  2. John Goche
    Replies:
    2
    Views:
    343
    Frederick Gotham
    Sep 4, 2006
  3. Afshin
    Replies:
    0
    Views:
    292
    Afshin
    Aug 12, 2011
  4. Afshin
    Replies:
    0
    Views:
    297
    Afshin
    Aug 12, 2011
  5. Afshin
    Replies:
    7
    Views:
    431
    Victor Bazarov
    Aug 16, 2011
Loading...

Share This Page