Thread: Can't understand this error

  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
    28,413
    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.
    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

  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,336
    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
    28,413
    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);
    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

  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, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM