Thread: Overloading >>

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    On further though, operator >> for string returns an istream&. The overload for your Car class expects a more specific derived class ifstream. Not all istreams are ifstreams, so there's a mismatch.

    It doesn't matter if you have used an istream before, if you are overloading operator >> use it. This way you can input into Car from files, console and stringstreams all the same.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #17
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Im still having problem with this. I changed it to istream, but when I tried to use it in a class that has inheritance im getting some trouble.

    Any ideas on how to solve this?

    I think the problem is the Car does not have a virtual >> function, as friend functions cant be virtual.

    Code:
    class Car
    {
          friend virtual std::istream  &operator>>(std::istream  &input, Car &c) = 0;  // < ----------- Does not work
          
          public:
                 Car(string n = "Defaul");
                 
          protected:
                  string name;
    };
    
    Car::Car(string n)
    {
       name = n;
    }
    
    class RaceCar : public Car
    {
          friend std::istream  &operator>>(std::istream  &input, RaceCar &rc);
          
          public:
                 RaceCar(string n = "Defaul", int speed = -1);
                 
          private:
                  int maxSpeed;
          
    };
    
    RaceCar::RaceCar(string n, int speed )
    {
       name = n;
       maxSpeed = speed;
    }
    
    std::istream &operator>>(std::istream &input, RaceCar &rc)
    {
        input >> rc.name >> rc.maxSpeed;
        return input;            
    }
    
    int main()
    {
        Car *carList[5];
        
        carList[0] = new RaceCar;
        
        ifstream fileData;
        fileData.open("info.txt");
        
        fileData >> carList[0];    // < ----------- Does not work
        
        delete carList[0];
    
        return 0;    
    }

  3. #18
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    carList[0] is a pointer to an object, not an object itself. Your operator>> reads from a stream into an object, not a pointer to an object.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The overload of operator>> is not a member function (and it cannot be, since you do not control std::istream). Consequently, it cannot a virtual member function, whether pure virtual or otherwise.

    What you can do is this:
    Code:
    class Car
    {
    public:
        friend std::istream& operator>>(std::istream& in, Car& car)
        {
            car.read(in);
            return in;
        }
    
        Car(const std::string& n = "Defaul") : name(n) {}
        virtual ~Car() {}
    protected:
        std::string name;
    
        virtual void read(std::istream& in) = 0;
    };
    
    class RaceCar : public Car
    {
    public:
        RaceCar(const std::string& n = "Defaul", int speed = -1)
            : Car(n), maxSpeed(speed) {}
    
    protected:
        virtual void read(std::istream& in)
        {
            in >> name >> maxSpeed;
        }
    
    private:
        int maxSpeed;
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Nice solution. Thanks a lot.

  6. #21
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    A consistent mistake in this thread: The second parameter of << should be const.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #22
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by King Mir View Post
    A consistent mistake in this thread: The second parameter of << should be const.
    Too bad everyone else is talking about operator >> not operator <<. So no mistake about that in this thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading << and >>
    By MMu in forum C++ Programming
    Replies: 1
    Last Post: 04-21-2008, 06:49 AM
  2. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  3. << and >> overloading
    By Armatura in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2003, 06:19 PM
  4. Overloading the << and >> operators
    By WebmasterMattD in forum C++ Programming
    Replies: 9
    Last Post: 10-15-2002, 05:22 PM
  5. istream >> overloading
    By wazza13 in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2002, 10:56 PM