Thread: Another overloading "<<" problem

  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    Another overloading "<<" problem

    I have two classes (Date and Student) linked and 've been trying to overload << for each, but never been successful
    Code:
    //*******************************
    //   Date.cpp
    //******************************
    #include "Date.h"
    .
    .
    .
    ofstream &operator<<(ofstream& stream, const Date& d){
    	stream << d.getMonth() << '|'
    		   << d.getDay() << '|'
    		   << d.getYear() << '|';
    
    	return stream;
    }
    .
    .
    .
    I include "Date.h" in "Student.h"
    The error is pointed to Student.cpp
    Code:
    //****************************
    //   Student.cpp
    //****************************
    #include "Student.h"
    .
    .
    ofstream &operator<<(ofstream& stream, const Student& s) {
    	stream << s.getFirstName() << '|'
    		<< s.getMiddleName() << '|'
    		<< s.getLastName() << '|'
    		<< s.getAddress1() << '|'
    		<< s.getAddress2() << '|'
    		<< s.getCity() << '|'
    		<< s.getState() << '|'
    		<< s.getZipCode() << '|'
    		<< s.getEnrollDate() << '|'    // error
    		<< s.getCreditHours() << '|';
    	return stream;
    }
    .
    .
    Here's the error
    E:\jDocument\CSCI\231\no1\Student.cpp(354) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'const class Date' (or there is no acceptable conversion)
    Again, I don't know if the problem lies above or somewhere else on my codes. I'll upload the whole file if necessary.
    thnx

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Is the overloaded << operator in your class declaration as a freind function?
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Isn't it a silly thing like you forgot to make a prototype in your header or you don't include the header of your Date class?

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Yup, they're both friend functions. Here are the headers.
    Code:
    //*********************************
    // Date.h
    //********************************
    #ifndef DATE_H
    #define DATE_H
    
    #include <fstream>
    using std::ofstream;
    using std::ifstream;
    
    class Date {
    public:
    	explicit Date(const int = 1, const int = 1, const int = 1900);
    
    	int getMonth(void) const;
    	int getDay(void) const;
    	int getYear(void) const;
    
    	void setMonth(const int);
    	void setDay(const int);
    	void setYear(const int);
    
    	// post-increment day
    	Date& operator++(void);
    
    	// pre-increment day
    	Date operator++(int);
    
    	friend ofstream &operator<<(ofstream&, const Date&);
    	friend ifstream &operator>>(ifstream&, Date&);
    
    	bool isLeapYear(int) const;
    	bool endOfMonth(int) const;
    private:
    	int month;
    	int day;
    	int year;
    
    	void incrementDay(void);
    };
    
    #endif
    
    //************************************
    Student.h
    //************************************
    #ifndef STUDENT_H
    #define STUDENT_H
    
    #include "Date.h"
    
    #include <fstream>
    using std::ofstream;
    using std::ifstream;
    
    #include <string>
    using std::string;
    
    class Student {
    public:
    	explicit Student(const string id = "000000000",
    			const string fn = "", const string mn = "", const string ln = "",
    			const string ad1 = "", const string ad2 = "",
    			const string cty = "", const string st = "", const string zip = "",
    			const int hrs = 0, const int yr = 1900, const int mth = 1, const int day = 1);
    	
    	string getStudentID(void) const;
    	string getFirstName(void) const;
    	string getMiddleName(void) const;
    	string getLastName(void) const;
    	string getAddress1(void) const;
    	string getAddress2(void) const;
    	string getCity(void) const;
    	string getState(void) const;
    	string getZipCode(void) const;
    	int getCreditHours(void) const;
    	Date getEnrollDate(void) const;
    	void setStudentID(const string);
    	void setFirstName(const string);
    	void setMiddleName(const string);
    	void setLastName(const string);
    	void setAddress1(const string);
    	void setAddress2(const string);
    	void setCity(const string);
    	void setState(const string);
    	void setZipCode(const string);
    	void setEnrollDate(const int m, const int d, const int yr);
    	void setCreditHours(const int);
    	void setEnrollDate(const Date);
    
    	void incrementCreditHours(const int);
    	Student &operator=(const Student&);
    
    	// based on studentID
    	bool operator!=(const Student&) const;
    	bool operator==(const Student&) const;
    
    	friend ofstream &operator<<(ofstream&, const Student&);
    	friend ifstream &operator>>(ifstream&, Student&);
    
    private:
    	string studentID;
    	string firstName;
    	string middleName;
    	string lastName;
    	string address1;
    	string address2;
    	string city;
    	string state;
    	string zipCode;
    	Date enrollDate;
    	int creditHours;
    };
    
    #endif

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Just a thought and probably not correct but your friend functions are declared under public: should they be?

  6. #6
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Looks like you should take out the const. I don't recall ever overloading << with the custom class as a const paramiter. I could be wrong but you should try it.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I tried putting the prototypes outside public and private, still no luck. Took out the const and the error is still there. Any more ideas?

  8. #8
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    You defined the operator << to use ofstream not ostream. Are you writing to a file?
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  9. #9
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Yup, I'm writing to a file

  10. #10
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Have you considered using the entire namespace std?
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  11. #11
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Tried it just now, not working still.

  12. #12
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Remove the f in ofstream and try it. I think that is why it is not working. It is not defined so to speak for ofstream anyways.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  13. #13
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I changed all ofstream's to ostream's in class DATE and it gets rid of the error, THANKS, dude! Can you explain, thou, why? I overloaded the ofstream operator because I planned to overload ostream operator as well. So that the formatting of the output for these two codes are different:
    outFile << Student; //write to a file
    cout << Student; // print to screen

    Anywayz, the fix would do it for now. Thanks

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    comment out the Student class. Focus on getting the Date class to respond to the << operator like you want. When you've done that, then comment out just the Date data member of Student and try to get the << operator to work the modified Student class. Then try adding back the Date member variable. Don't try output the Date member variable of Student using the << operator to begin. Just use the public access method. Then try putting the public access method for the Date member variable in the definition of the << operator for the Student class. This approach will help you figure out where the problem is.

  15. #15
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    The ofstream inherits the ostream. I forgot that earlier.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator overloading slight problem
    By reddzer in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2006, 04:30 PM
  2. oops? not sure. operator overloading (possible) problem
    By w00tw00tkab00t in forum C++ Programming
    Replies: 8
    Last Post: 02-08-2006, 05:38 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM