0

I have a Point2D class

Point2D

class Point2D
{
 public:

           int getX() const;
           int getY() const;

           void setX(int);
           void setY(int);

 protected:

             int x;
             int y;

};

I am trying to overload the output operator << to work with Point2D iterator p2d_list.begin() but i am unsure how to do it , this is my attempt

ostream& operator<< (ostream& afile, Point2D* p2)
{

               afile<<left
                    <<setw(1)
                    <<"["
                    <<left
                    <<setw(5)
                    <<p2->getX()
                    <<" ,"
                    <<left
                    <<setw(5)
                    <<p2->getY()
                    <<" ]    "
                    <<left
                    <<setw(6)
                    <<p2->getScalarValue()
                    <<endl;

                return afile;


}

In my main function

int main()
{

   list<Point2D> p2d_list;

   list<Point2D>::iterator p2 = p2d_list.begin();

        while ( p2 != p2d_list.end() )
        {
            cout<<p2; //error here
            p2++;

        }

}

I am getting the following Error

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::_List_iterator<_Mylist>' (or there is no acceptable conversion)

How do i overload the output operator << so that it works with Point2D iterator **p2d_list.begin()

2 Answers 2

2

You are trying to output iterator, not Point2D. You should dereference iterator first. In your case it should be

cout << &(*p2);

, but I have no idea, why your operator << receives pointer, instead of ref/const-ref. It should be

ostream& operator<< (ostream& afile, const Point2D& p2)

and then output should be

cout << *p2;
Sign up to request clarification or add additional context in comments.

4 Comments

what changes must i make to the overload function?? and how should i cout ??
@Computernerd use ., instead of -> when address to fields of object. It's all.
it doesnt work i am getting the same error: Error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Point2D' (or there is no acceptable conversion)
Show code. Is your operator << declaration changed as I advice?
0

You need to overload the operator for list<Point2D>::iterator type instead of Point2D*

ostream& operator<< (ostream& afile, list<Point2D>::iterator &p2)
{

           afile<<left
                <<setw(1)
                <<"["
                <<left
                <<setw(5)
                <<(*p2)->getX()
                <<" ,"
                <<left
                <<setw(5)
                <<(*p2)->getY()
                <<" ]    "
                <<left
                <<setw(6)
                <<(*p2)->getScalarValue()
                <<endl;

            return afile;


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.