Overloading >>

This is a discussion on Overloading >> within the C++ Programming forums, part of the General Programming Boards category; On further though, operator >> for string returns an istream& . The overload for your Car class expects a more ...

  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,801
    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
    21,743
    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;
    };
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,037
    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.

Page 2 of 2 FirstFirst 12
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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21