overloaded >> operator problem

This is a discussion on overloaded >> operator problem within the C++ Programming forums, part of the General Programming Boards category; I am trying to overload an the >> operator for my new Money class. It compiles fine, however when it ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    45

    overloaded >> operator problem

    I am trying to overload an the >> operator for my new Money class. It compiles fine, however when it gets to the point where i use the operator it, seems to loop the input method for a while and then end the program skipping any code after it. Any ideas?

    Code:
    money.h
    
    class Money
    {
        private:
            int cents;
    
        public:
            Money();
            Money(int cent);
    
            void input(istream &in) const;
    };
    
    istream & operator>> (istream & in, const Money & m);
    Code:
    money.cpp
    
    Money::Money(){}
    
    Money::Money(int cent)
    {
        cents = cent;
    }
    
    void Money::input(istream &in) const
    {
        //THIS IS WHERE IT LOOPS UNTIL ENDING THE PROGRAM
        in >> cents;
    }
    
    istream& operator>>(istream & in, const Money &m)
    {
       m.input( in );
       return in;
    }
    Code:
    testMoney.cpp
    
    int main()
    {
        Money m();
        cin >> m;
    }
    The program never allows you to enter anything from the cin >> cents command. I have more code, but i am trying to simplify it...if you need to see more, let me know.

    Thanks

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Im pretty sure you are suppose to add the operator function inside the class, and it is suppose to return the class(in your case its return type is suppose to be Money)

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    That would be the case if I wasn't making the input() function a member method and just passing the istream to the overloaded function. Thanks for looking

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,660
    >> It compiles fine...
    Not what you've posted. The input() method is const and therefore can't modify 'cents'.

    >> ...but i am trying to simplify it...
    Once you think you've simplified it. Compile and run it to make sure it still demonstrates the issue.

    gg

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    1st you need to make your overload a friend function.
    2nd you don't need the input() AND the operator>> overload functions unless you just are trying to create dual methods for input.
    3rd The Money parameter, shouldn't be a constant, because you are trying to change it with the inputted value.
    4th, I added the namespace std:: but I imagine you have just omitted that you are using namespace std since you say it compiles.

    Code:
    class Money
    {
    	friend std::istream& operator>>(std::istream & in, Money &m);
    
        private:
            int cents;
    
        public:
            Money();
            Money(int cent);
    };
    Code:
    Money::Money(){}
    
    Money::Money(int cent)
    {
        cents = cent;
    }
    
    
    std::istream& operator>>(std::istream & in, Money &m)
    {
    	int amount = 0;
    	in >> amount;
    	m.cents = amount;
    	return in;
    }
    Last edited by Darryl; 07-19-2005 at 03:17 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Your operator>> should not take a const Money reference, it should take a plain Money reference sine it will modify the variable. Also, the input member function should not be const.

    In your example code you should declare a new instance of money without the parentheses (i.e. use Money m; ). The way you have it declares a function instead of a variable.

    The const thing might be what's causing the problem, since it compiles but fails on the platform I tested. But if it isn't, it is likely due to other code messing up the stream state.

    Darryl, just making operator>> a friend might be preferred, but there's nothing wrong with having an input function that does the work instead.
    Last edited by Daved; 07-19-2005 at 03:13 PM.

  7. #7
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by Daved

    Darryl, just making operator>> a friend might be preferred, but there's nothing wrong with having an input function that does the work instead.
    I would agree if input() was able to stand on it's own, but in this case, it appears the input() is only a work around for not using friend. Using friend is the simpler and more effiecient.

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    thanks everyone, removing the const worked. I am not using a friend decleration because this is for a class and i must demonstrate that i understand what the teacher taught . sorry codeplug for not being more thorough, and the m() was a typo, so everything compiles and runs as desired

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. Friend func. and overloaded >> and <<
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2005, 12:14 AM
  3. overloaded >> operator issue...
    By soulredemption in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 10:53 PM
  4. Overloaded >>
    By alphaoide in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2004, 12:27 AM
  5. overloaded on overloads
    By emceedee in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2003, 01:14 AM

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