Help with Operator Overloading

This is a discussion on Help with Operator Overloading within the C++ Programming forums, part of the General Programming Boards category; Hi there, I'm trying to use Operator Overloading to save some objects into a text file using the >> operator. ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Help with Operator Overloading

    Hi there,

    I'm trying to use Operator Overloading to save some objects into a text file using the >> operator.

    The program is compiling, but instead of saving the objects into the file, it is currently displaying the pointers address to the console.

    I've never used Operator Overloading before, so it's a new concept to me.

    I'm trying to overload the >> operator for the derived class 'Leads'.

    Here is the program:

    header file (diary.h) :

    Code:
    using namespace std;
    #include <string>
    
    class Diary
    {
    public:
    
    	Diary(string mainname, string mainaddress, string  mainpostcode, string maintelno, string maindetails);
    	//friend ostream &operator<<(ostream &streamname, Diary obj);
    
    private:
    
    protected:
    
    	string m_Name;
    	string m_Address;
    	string m_PostCode;
    	string m_TelNo;
    	string m_Details;
    
    };
    
    Diary::Diary(string mainname, string mainaddress, string  mainpostcode, string maintelno, string maindetails) 
    {
    	m_Name = mainname;
    	m_Address = mainaddress;
    	m_PostCode = mainpostcode;
    	m_TelNo = maintelno;
    	m_Details = maindetails;	
    }
    
    
    //ostream &operator<<(ostream &streamname, Diary obj)
    //{
    //	streamname << obj.m_Name << " " << obj.m_Address << " " << obj.m_PostCode << " " << obj.m_TelNo << " " << obj.m_Details << endl;
    //	return streamname;
    //}
    
    
    
    //----------------------------------------------------------------
    class Leads : public Diary
    {
    public:
    
    	Leads(string m_Name, string m_Address, string m_PostCode, string m_TelNo, string m_Details, string m_Date, string m_Time);
    
    	friend ostream &operator<<(ostream &streamname, Leads obj);
    
    
    
    private:
    
    protected:
    
    	string m_Date;
    	string m_Time;
    
    };
    Leads::Leads(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails, string maindate, string maintime) : Diary(mainname, mainaddress, mainpostcode, maintelno, maindetails) 
    {
    	m_Date = maindate;
    	m_Time = maintime;
    }
    
    ostream &operator<<(ostream &streamname, Leads obj)
    {
    	streamname << obj.m_Name << " " << obj.m_Address << " " << obj.m_PostCode << " " << obj.m_TelNo << " " << obj.m_Details << endl;
    	return streamname;
    }

    main() :

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream> 
    #include "diary.h"
    
    using namespace std;
    
    int main ()
    {
    	vector<Diary *>vectorname;
    
    	do{
    		int choice = 0;
    		cout << "Choose one of the options below:" << endl;
    		cout << "1) Enter a lead" << endl;
    		cout << "2) Exit the program" << endl;
    		cin >> choice;
    		cin.ignore();
    
    		switch(choice)
    		{
    		case 1:
    			{
    				cout << "You chose to enter a lead" << endl;
    				//---------------------
    				//Collect the leads details
    				//---------------------
    				string mainname;
    				cout << "Name? " << endl;
    				getline (cin, mainname);
    
    				string mainaddress;
    				cout << "Address? " << endl;
    				getline (cin, mainaddress);
    
    				string mainpostcode;
    				cout << "Post Code? " << endl;
    				getline (cin, mainpostcode);
    
    				string maintelno;
    				cout << "Telephone Number? " << endl;
    				getline (cin, maintelno);
    
    				string maindetails;
    				cout << "Details? " << endl;
    				getline (cin, maindetails);
    
    				string maindate;
    				cout << "Date ? DDMMYY " << endl;
    				getline (cin, maindate);
    
    				string maintime;
    				cout << "Time? 24hr HHMM " << endl;
    				getline (cin, maintime);
    
    				cout << "All questions asked" << endl;
    
    				try
    				{
    					Leads *ptr = new Leads(mainname, mainaddress, mainpostcode, maintelno, maindetails, maindate, maintime);
    					vectorname.push_back( ptr );
    				}
    				catch (bad_alloc& ba)
    				{
    					cerr << "bad_alloc caught: " << ba.what() << endl;
    				}
    
    				cout << "past push Lead to list and before deletion" << endl;
    				break;
    			}
    
    		case 2:
    			{
    				cout << "You chose to exit the program" << endl;
    
    				//Save all the objects to the text file:
    
    				ofstream streamname("database.txt"); 
    				if(!streamname) { 
    					cout << "Cannot open file" << endl; 
    					return 1; 
    				} 
    
    
    				for(int a = 0; a < vectorname.size() ; a++)
    				{
    					//save all objects to the database.txt file
    
    					cout << vectorname[a];
    				}
    
    				streamname << "Some text here" << endl; 
    				streamname.close();
    
    				//Iterate over the vector to delete all the pointers.
    				//Exit the program
    
    				break;
    			}
    		}
    	}
    	while(true);
    
    	system ("PAUSE");
    
    	return 0;
    }
    Thanks for any help with this. It's most appreciated

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You define the operator on an object, and yet you call << on a pointer.... (EDIT: And since a pointer is not an overloadable type, that means you need to call << on an object.)

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    instead of saving the objects into the file, it is currently displaying the pointers address to the console.

    Your problem is here.. and I think we both know what needs to be done:
    Code:
    //ostream &operator<<(ostream &streamname, Diary obj)
    //{
    //	streamname << obj.m_Name << " " << obj.m_Address << " " << obj.m_PostCode << " " << obj.m_TelNo << " " << obj.m_Details << endl;
    //
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks tabstop.

    That gives me some concern.

    I'm actually storing pointers to objects you see within a vector, so can I ask what would be the right way of being able to save all the objects to the text file?

    I'm not asking for the code or anything, but I'd really appreciate some direction on how to go about being able to save them.

    Again, thanks.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Swerve View Post
    Thanks tabstop.

    That gives me some concern.

    I'm actually storing pointers to objects you see within a vector, so can I ask what would be the right way of being able to save all the objects to the text file?

    I'm not asking for the code or anything, but I'd really appreciate some direction on how to go about being able to save them.

    Again, thanks.
    I'm concerned about your concern. If vectorname[a] is a pointer to a Diary (and we know it is), then *(vectorname[a]) is a Diary object. This is not difficult.

    (EDIT: Now what will be difficult is trying to do polymorphism, which I think you're trying to do, using a base object as a parameter (instead of calling a member function from the pointer-to-base). In fact, I'm moderately certain it can't be done in C++, or at least I haven't thought up/remembered a way to do it yet.)
    Last edited by tabstop; 10-23-2009 at 06:20 PM.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks chaps.

    Now what will be difficult is trying to do polymorphism, which I think you're trying to do, using a base object as a parameter (instead of calling a member function from the pointer-to-base). In fact, I'm moderately certain it can't be done in C++, or at least I haven't thought up/remembered a way to do it yet.
    Yikes!!

    Maybe I shouldn't even be trying to use operator overloading for this then.

    All I'm trying to do is save the derived objects to the text file. Perhaps I wrongly thought this as the right way to do it.

    Right now the program keeps calling the base classes definition of the >> operator.

    Think I'll try a different method then.

    Many thanks

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    You may want to consider passing your Diary and Leads objects by const-reference instead of by value.
    I believe you're slicing objects at the moment.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

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