Can't understand this error

This is a discussion on Can't understand this error within the C++ Programming forums, part of the General Programming Boards category; Code: // Overide operator >> to read date std::istream& operator>>(std::istream& in, const Date& date) { in >> date.year; in >> ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Can't understand this error

    Code:
    // Overide operator >> to read date
    std::istream& operator>>(std::istream& in, const Date& date)
    {
    	in >> date.year;
    	in >> date.month;
    	in >> date.day;
    	in >> date.hour;
    	in >> date.minute;
    	
    	return in;
    }
    
    // ERROR: : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)
    Date Class
    Code:
    #ifndef DATE_H
    #define DATE_H
    
    #include <time.h>
    #include <iostream>
    
    class Date						
    {
    public:
    
    	Date();
    
    	void SetDate(tm curr_tm);
    	static tm getCurrentTime();
    	int getDay(){return day;}
    	int getMonth(){return month;}
    	int getYear(){return year;}
    	int getHour(){return hour;}
    	int getMinute(){return minute;}
    	tm getLocalDT(){return local_dt;}
    	friend std::istream& operator>>(std::istream& in, const Date& date);
    	friend std::ostream& operator<<(std::ostream& out, const Date& date);
    	
    private:
    
    	int day;
    	int month;
    	int year;
    	int hour;
    	int minute;
    	tm local_dt;
    };
    
    
    #endif;
    ???????
    All the members of Date are declared as int
    Last edited by csonx_p; 08-24-2008 at 10:25 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Change:
    Code:
    std::istream& operator>>(std::istream& in, const Date& date)
    to:
    Code:
    std::istream& operator>>(std::istream& in, Date& date)
    After all, it does not make sense to try and read into something that you are not allowed to change.
    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

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by laserlight View Post
    Change:
    Code:
    std::istream& operator>>(std::istream& in, const Date& date)
    to:
    Code:
    std::istream& operator>>(std::istream& in, Date& date)
    After all, it does not make sense to try and read into something that you are not allowed to change.
    Interesting that most of the Date examples i've seen have this as a const ...

    Other related questions

    1. If i do the following i get a 'recursive error which may cause memory overflow'

    Code:
    // Overide operator >> to read date
    std::istream& operator>>(std::istream& in, const Date& date)
    {
    	in >> date;
    	
    	return in;
    }
    Is it also because of const?

    2. What's the advantage of having this overloading outside the class as a free function, or like i have it (friend), or have it as static function inside the class?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by csonx_p View Post
    Interesting that most of the Date examples i've seen have this as a const ...

    Other related questions

    1. If i do the following i get a 'recursive error which may cause memory overflow'

    Code:
    // Overide operator >> to read date
    std::istream& operator>>(std::istream& in, const Date& date)
    {
    	in >> date;
    	
    	return in;
    }
    Is it also because of const?

    2. What's the advantage of having this overloading outside the class as a free function, or like i have it (friend), or have it as static function inside the class?
    No, it's because in >> date is going to call operator >> -- the same operator >> you are defining. You're going to need to break date into its parts and call >> on those.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Interesting that most of the Date examples i've seen have this as a const ...
    Can't comment on that one... It's incorrect to use a const reference to a operator>> since its purpose is to change the content of the second variable.
    Other related questions

    1. If i do the following i get a 'recursive error which may cause memory overflow'

    Code:
    // Overide operator >> to read date
    std::istream& operator>>(std::istream& in, const Date& date)
    {
    	in >> date;
    	
    	return in;
    }
    Is it also because of const?
    Nope, it's because
    Code:
    	in >> date;
    becomes:
    Code:
    	opertor>>(in, date);
    which is the same function you are in - so it's infinitely calling itself until the stack overflows or the application terminates for another reason - most likely the stack will fill up first of those two things (unless you are really quick on the CTRL-C keypress).

    2. What's the advantage of having this overloading outside the class as a free function, or like i have it (friend), or have it as static function inside the class?
    There isn't much advantage either way - both are valid and generate the same code in the end.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by csonx_p
    Interesting that most of the Date examples i've seen have this as a const ...
    Perhaps you have been looking at overloading operator<< instead:
    Code:
    std::ostream& operator<<(std::ostream& out, const Date& date);
    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

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by laserlight View Post
    Perhaps you have been looking at overloading operator<< instead:
    Code:
    std::ostream& operator<<(std::ostream& out, const Date& date);
    !Oh u right ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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