Question

I have a Point2D class and i am trying to overload the input operator >>

class Point2D
{
 public:
           Point2D(int,int);
           int getX();
           int getY();

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

           double getScalarValue();

          bool operator < ( const Point2D& x2) const
          {
            return x < x2.x;
          }

              friend istream& operator >> (istream&,Point2D);


 protected:

             int x;
             int y;
             double distFrOrigin;
             void setDistFrOrigin();


};

Outside my main function

    #include <iostream>
    #include <fstream>
    #include "Line2D.h"
    #include "MyTemplates.h"
    #include <string>
    #include <set>

    using namespace std;

    istream operator >> (istream& is , Point2D p2d)
    {
        string p;
        getline(is,p,'\n');
       int position = p.find(", ");

        string k = p.substr(0,position);

       if ( k == "Point2D")
       {
         string x = p.substr(10,1);
         int x_coordinate = atoi(x.c_str()); // atoi(x.c_str()) convert string x to int
         p2d.setX(x_coordinate);

       }

       return is;
    }

In my int main()

    int main()
{

   fstream afile;
   string p;
   afile.open("Messy.txt",ios::in);

   if (!afile)
   {
     cout<<"File could not be opened for reading";
     exit(-1);
   }

   Point2D abc;

   afile>>abc;

   set<Point2D> P2D;
   P2D.insert(abc);

   set<Point2D>::iterator p2 = P2D.begin();

   while ( p2 != P2D.end() )
   { 
     cout<<p2->getX();
     p2++;
   }

}

I dont understand why am i getting an error:

c++ forbids declaration of istream with no type

I have already included iostream , fstream , using namespace std , i cant figure out whats wrong

Était-ce utile?

La solution

You have several issues in your code.

  1. This is the reason for your error message. You don't #include <iostream> in Point2D.h/Line2D.h.

    As others already suggested, use std::istream instead of just istream.

  2. The correct operator>>() should be

    std::istream &operator>>(std::istream &is, Point2D &p2d);
    

    Note the std::istream& and the Point2D&. Point2D& is important, because otherwise you modify a local copy and the given parameter remains unchanged.

  3. Your input operator is fragile. It is vulnerable to having more or less whitespace. You also allow exactly one digit for x_coordinate. Additionally, you skip one character beyond Point2D,. A better approach could be to separate the parts with just whitespace and let the iostream library handle the parsing. E.g.

    Point2D 15 28
    

    could be read by

    string tag;
    is >> tag;
    if (tag == "Point2D")
        is >> p2d.x >> p2d.y;
    
    return is,
    

Autres conseils

The thing that pops out (as Dietmar Kühl mentioned) is that you aren't returning a reference of istream. Try changing this method header

istream operator >> (istream& is , Point2D p2d)

to this:

istream& operator >> (istream& is , Point2D p2d)

As a rule of thumb returning a reference to an object that you haven't made a copy constructor for is usually a good idea. Returning a reference means that you are returning the address that the object is located. To correctly return by value, there needs to be a copy made of the object being returned.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top