Overloading stream operator <<. Return memory address instead object
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Overloading stream operator <<. Return memory address instead object

  1. #1
    Join Date
    Jun 2012
    Posts
    127

    Overloading stream operator <<. Return memory address instead object

    Subj. Try to implement overloading << operator. If I done it void then everything work fine (see comment out) if I make it class of ostream& then the operator return to me some memory address. Please help

    Code:
    #ifndef Point_HPP // anti multiply including gates
    #define Point_HPP
    
    #include <sstream>
    	
    
    class Point
    {	
    	private:		//  declaration of private data members 
    	double x;		// X coordinate
    	double y;		// Y coordinate
    
    	public: // public declaration of data members (in given example haven't ) and member functions 
    
    
    		//----------- Declaration of Constructors -----------//
    	Point();									// Default constructor
    	Point(double newX, double newY);			// Constructor 
    	Point (const Point& ObjectOfClassPoint);	//COPY constructor
    	~Point(); // 
    
    
    	//----------- Declaration of  Accessors member functions -----------//
    
    	std::ostream& ToString() ; 
    	//void Point::ToString () const; 
    	
    	
    };
    
    
    //----------- Implementaion of Global Ostream << Operator  -----------//
    
    /*std::ostream& operator<< (std::ostream &out, Point &cPoint) 
    {
        // SHOULD BE WITHOUT friendness 
        // Point's members directly.
    	out << cPoint.ToString();
            
        return out;
    }*/
    
    #endif // Point_HPP
    Code:
    
    #include <iostream>
    #include "Point.hpp"
    
    
    
    
    int main()
    
    	{
    
    
    	std::cout << "\n\t ---TEST OF Operator Overloading--- \n";
    	
    	Point MyPoint(1455  , 1492  );							// Creating an object of  Point using constructor 
    	Point MySecondPoint (1517   , 1796  );					// Creating an object of  Point using constructor 
    	Point MyThirdPoint (1610  , 1882   );					// Creating an object of  Point using constructor 
    	MyPoint.ToString();
    	MySecondPoint.ToString();
    	std::cout << "\n";
    
    
    	std::cout << "\n std::cout << MySecondPoint.ToString();;\n\n\n ";
    	std::cout << "\n" <<  MySecondPoint.ToString();
    
    
    	std::cout << "\n ---  Now some easy math --- ";
    	
    
      return 0 ; 
    }
    Code:
    #include "Point.hpp"
    #include <iostream>
    #include <sstream>
    #include <cmath>
    
    
    
    
    
    			//----------- Implementation of Constructors -----------//
    
    Point::Point() : x(0), y(0)															// Default constructor (implemented using colon syntax )
    { 
    	std::cout << "hi my default constructor\n\n\t";
    }										
    Point::Point(double newX, double newY) : x(newX), y(newY)							// Constructor 
    { 
    	//std::cout << "hi my constructor\n\n\t"; 
    }				
    Point::~Point() 																	// Destructor
    {
    	//std::cout << "bye my point..\n";
    }									 
    
    Point::Point (const Point& ObjectOfClassPoint)										// Copy constructor
    		{
    			//std::cout << "this is COPY constructor\n\n\t ";
    			x = ObjectOfClassPoint.x;
    			y = ObjectOfClassPoint.y;
    
    		}
    
    
    		
    
    			//----------- Implementation of Accessor(s) member functions -----------//
    
    std::ostream& Point::ToString ()
    	
    	{// Function ToString should also be const also because of reason of mistaken modification of an object's value 
    		std::ostringstream os;								// std::stringstream object
    		os << " Point (" << x << ", " << y << ")\n";			// customization of output 
    		os.str();											// str() function retrieve the string from the string buffer
    		return os;
    
    	}
    
    
    /*void Point::ToString () const
    	
    	{// Function ToString should also be const also because of reason of mistaken modification of an object's value 
    		std::ostringstream os;								// std::stringstream object
    		os << " Point (" << x << ", " << y << ")";			// customization of output 
    		std::cout << os.str()<<"\n";						// str() function retrieve the string from the string buffer
    	}
    	*/

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,646

    Re: Overloading stream operator <<. Return memory address instead object

    1) your ToString() function is returning a reference to a local object,
    so that is incorrect from the start. Also it should return a string, not
    an ostream.

    2) Your code commented out operator <<, so you are not testing operator << at all.

    3) Your operator should look something like this:

    Code:
    std::ostream& operator<< (std::ostream &out, const Point &cPoint) 
    {
        // Point's members directly.
    	out << cPoint.ToString();
            
        return out;
    }

  3. #3
    Join Date
    Jun 2012
    Posts
    127

    Re: Overloading stream operator <<. Return memory address instead object

    Code was updated

    Code:
    #ifndef Point_HPP // anti multiply including gates
    #define Point_HPP
    
    #include <sstream>
    	
    
    class Point
    {	
    	private:		//  declaration of private data members 
    	double x;		// X coordinate
    	double y;		// Y coordinate
    
    	public: // public declaration of data members (in given example haven't ) and member functions 
    
    
    		//----------- Declaration of Constructors -----------//
    	Point();									// Default constructor
    	Point(double newX, double newY);			// Constructor 
    	Point (const Point& ObjectOfClassPoint);	//COPY constructor
    	~Point(); // 
    
    
    	//----------- Declaration of  Accessors member functions -----------//
    
    	std::string Point::ToString ();
    	//void Point::ToString () const; 
    	
    	
    };
    
    
    //----------- Implementaion of Global Ostream << Operator  -----------//
    
    std::ostream& operator<< (std::ostream &out, const Point &cPoint) 
    {
        // SHOULD BE WITHOUT friendness 
        // Point's members directly.
    
    	out << cPoint.ToString();
            
        return out;
    }
    
    #endif // Point_HPP
    Code:
    
    #include <iostream>
    #include "Point.hpp"
    #include <sstream>
    
    
    
    
    int main()
    
    	{
    
    
    	std::cout << "\n\t ---TEST OF Operator Overloading--- \n";
    	
    	Point MyPoint(1455  , 1492  );							// Creating an object of  Point using constructor 
    	Point MySecondPoint (1517   , 1796  );					// Creating an object of  Point using constructor 
    	Point MyThirdPoint (1610  , 1882   );					// Creating an object of  Point using constructor 
    	MyPoint.ToString();
    	MySecondPoint.ToString();
    	std::cout << "\n";
    
    
    	std::cout << "\n std::cout << MySecondPoint.ToString();;\n\n\n ";
    	std::cout << "\n" <<  MySecondPoint.ToString();
    
    
    	std::cout << "\n ---  Now some easy math --- ";
    
    	std::cout << "\n MySecondPoint;\n\n\n ";
    	std::cout << MySecondPoint;
    	
    
      return 0 ; 
    }
    Code:
    #include "Point.hpp"
    #include <iostream>
    #include <sstream>
    #include <cmath>
    
    
    
    
    
    			//----------- Implementation of Constructors -----------//
    
    Point::Point() : x(0), y(0)															// Default constructor (implemented using colon syntax )
    { 
    	std::cout << "hi my default constructor\n\n\t";
    }										
    Point::Point(double newX, double newY) : x(newX), y(newY)							// Constructor 
    { 
    	//std::cout << "hi my constructor\n\n\t"; 
    }				
    Point::~Point() 																	// Destructor
    {
    	//std::cout << "bye my point..\n";
    }									 
    
    Point::Point (const Point& ObjectOfClassPoint)										// Copy constructor
    		{
    			//std::cout << "this is COPY constructor\n\n\t ";
    			x = ObjectOfClassPoint.x;
    			y = ObjectOfClassPoint.y;
    
    		}
    
    
    		
    
    			//----------- Implementation of Accessor(s) member functions -----------//
    
    std::string Point::ToString ()
    {// Function ToString should also be const also because of reason of mistaken modification of an object's value 
        std::ostringstream os;                        // std::stringstream object
        os << "Point (" << x << ", " << y << ")"; // customization of output 
        return os.str();                                  // str() function retrieve the string from the string buffer
    }
    
    
    /*void Point::ToString () const
    	
    	{// Function ToString should also be const also because of reason of mistaken modification of an object's value 
    		std::ostringstream os;								// std::stringstream object
    		os << " Point (" << x << ", " << y << ")";			// customization of output 
    		std::cout << os.str()<<"\n";						// str() function retrieve the string from the string buffer
    	}
    	*/
    with key word cost compiler give me
    1>------ Build started: Project: L4. Customizing stream operator, Configuration: Release Win32 ------
    1> main.cpp
    1>c:\all my\с++\laboratory\laboratory\l4. customizing stream operator\Point.hpp(39): error C2662: 'Point::ToString' : cannot convert 'this' pointer from 'const Point' to 'Point &'
    1> Conversion loses qualifiers
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    without const

    1>------ Build started: Project: L4. Customizing stream operator, Configuration: Release Win32 ------
    1> Point.cpp
    1>Point.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Point &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVPoint@@@Z) already defined in main.obj
    1>C:\all my\с++\Laboratory\Laboratory\Release\L4. Customizing stream operator.exe : fatal error LNK1169: one or more multiply defined symbols found
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,646

    Re: Overloading stream operator <<. Return memory address instead object

    1) ToString() should be a const member function (you have that version
    commented out ... why?)

    2) You should only put the declaration of operator << in the header file. The implementation
    should be in the CPP file.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Overloading stream operator <<. Return memory address instead object

    Code:
    Point::Point (const Point& ObjectOfClassPoint)										// Copy constructor
    		{
    			//std::cout << "this is COPY constructor\n\n\t ";
    			x = ObjectOfClassPoint.x;
    			y = ObjectOfClassPoint.y;
    
    		}
    Sigh. Still writing copy constructors when you don't need to? I guess all that I stated in this thread didn't mean anything to you:

    http://forums.codeguru.com/showthrea...t=#post2077581

    When learning C++, you're supposed to build upon what you learned previously. You don't just throw away what you learn and then start fresh making the same mistakes.

    This will become annoying if we spend time explaining your mistakes to you, and then you go start new discussion threads making the same mistakes we just spent our time discussing with you.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 27th, 2012 at 12:25 AM.

  6. #6
    Join Date
    Jun 2012
    Posts
    127

    Re: Overloading stream operator <<. Return memory address instead object

    Quote Originally Posted by Paul McKenzie View Post
    Code:
    Point::Point (const Point& ObjectOfClassPoint)										// Copy constructor
    		{
    			//std::cout << "this is COPY constructor\n\n\t ";
    			x = ObjectOfClassPoint.x;
    			y = ObjectOfClassPoint.y;
    
    		}
    Sigh. Still writing copy constructors when you don't need to? I guess all that I stated in this thread didn't mean anything to you:

    http://forums.codeguru.com/showthrea...t=#post2077581

    When learning C++, you're supposed to build upon what you learned previously. You don't just throw away what you learn and then start fresh making the same mistakes.

    This will become annoying if we spend time explaining your mistakes to you, and then you go start new discussion threads making the same mistakes we just spent our time discussing with you.

    Regards,

    Paul McKenzie
    There is a rule toward my home assignments that functionality from previous exercise should be in next exercise. However, I will do delete all extra functionality before I start new thread in order do not annoy you.
    Best regards,
    Denis

  7. #7
    Join Date
    Jun 2012
    Posts
    127

    Re: Overloading stream operator <<. Return memory address instead object

    Quote Originally Posted by Philip Nicoletti View Post
    1) ToString() should be a const member function (you have that version
    commented out ... why?)

    2) You should only put the declaration of operator << in the header file. The implementation
    should be in the CPP file.
    Thank you for giving attention. Of course should be const. I just made realization ToString() that return void that decide that I wanna make ToString that return string and forgot about const-ness. Silly mistake...

  8. #8
    Join Date
    Jun 2012
    Posts
    127

    Re: Overloading stream operator <<. Return memory address instead object

    Quote Originally Posted by oteel View Post
    Code was updated

    Code:
    #ifndef Point_HPP // anti multiply including gates
    #define Point_HPP
    
    #include <sstream>
    	
    
    class Point
    {	
    	private:		//  declaration of private data members 
    	double x;		// X coordinate
    	double y;		// Y coordinate
    
    	public: // public declaration of data members (in given example haven't ) and member functions 
    
    
    		//----------- Declaration of Constructors -----------//
    	Point();									// Default constructor
    	Point(double newX, double newY);			// Constructor 
    	Point (const Point& ObjectOfClassPoint);	//COPY constructor
    	~Point(); // 
    
    
    	//----------- Declaration of  Accessors member functions -----------//
    
    	std::string Point::ToString ();
    	//void Point::ToString () const; 
    	
    	
    };
    
    
    //----------- Implementaion of Global Ostream << Operator  -----------//
    
    std::ostream& operator<< (std::ostream &out, const Point &cPoint) 
    {
        // SHOULD BE WITHOUT friendness 
        // Point's members directly.
    
    	out << cPoint.ToString();
            
        return out;
    }
    
    #endif // Point_HPP
    Code:
    
    #include <iostream>
    #include "Point.hpp"
    #include <sstream>
    
    
    
    
    int main()
    
    	{
    
    
    	std::cout << "\n\t ---TEST OF Operator Overloading--- \n";
    	
    	Point MyPoint(1455  , 1492  );							// Creating an object of  Point using constructor 
    	Point MySecondPoint (1517   , 1796  );					// Creating an object of  Point using constructor 
    	Point MyThirdPoint (1610  , 1882   );					// Creating an object of  Point using constructor 
    	MyPoint.ToString();
    	MySecondPoint.ToString();
    	std::cout << "\n";
    
    
    	std::cout << "\n std::cout << MySecondPoint.ToString();;\n\n\n ";
    	std::cout << "\n" <<  MySecondPoint.ToString();
    
    
    	std::cout << "\n ---  Now some easy math --- ";
    
    	std::cout << "\n MySecondPoint;\n\n\n ";
    	std::cout << MySecondPoint;
    	
    
      return 0 ; 
    }
    Code:
    #include "Point.hpp"
    #include <iostream>
    #include <sstream>
    #include <cmath>
    
    
    
    
    
    			//----------- Implementation of Constructors -----------//
    
    Point::Point() : x(0), y(0)															// Default constructor (implemented using colon syntax )
    { 
    	std::cout << "hi my default constructor\n\n\t";
    }										
    Point::Point(double newX, double newY) : x(newX), y(newY)							// Constructor 
    { 
    	//std::cout << "hi my constructor\n\n\t"; 
    }				
    Point::~Point() 																	// Destructor
    {
    	//std::cout << "bye my point..\n";
    }									 
    
    Point::Point (const Point& ObjectOfClassPoint)										// Copy constructor
    		{
    			//std::cout << "this is COPY constructor\n\n\t ";
    			x = ObjectOfClassPoint.x;
    			y = ObjectOfClassPoint.y;
    
    		}
    
    
    		
    
    			//----------- Implementation of Accessor(s) member functions -----------//
    
    std::string Point::ToString ()
    {// Function ToString should also be const also because of reason of mistaken modification of an object's value 
        std::ostringstream os;                        // std::stringstream object
        os << "Point (" << x << ", " << y << ")"; // customization of output 
        return os.str();                                  // str() function retrieve the string from the string buffer
    }
    
    
    /*void Point::ToString () const
    	
    	{// Function ToString should also be const also because of reason of mistaken modification of an object's value 
    		std::ostringstream os;								// std::stringstream object
    		os << " Point (" << x << ", " << y << ")";			// customization of output 
    		std::cout << os.str()<<"\n";						// str() function retrieve the string from the string buffer
    	}
    	*/
    with key word cost compiler give me
    1>------ Build started: Project: L4. Customizing stream operator, Configuration: Release Win32 ------
    1> main.cpp
    1>c:\all my\с++\laboratory\laboratory\l4. customizing stream operator\Point.hpp(39): error C2662: 'Point::ToString' : cannot convert 'this' pointer from 'const Point' to 'Point &'
    1> Conversion loses qualifiers
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    without const

    1>------ Build started: Project: L4. Customizing stream operator, Configuration: Release Win32 ------
    1> Point.cpp
    1>Point.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Point &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVPoint@@@Z) already defined in main.obj
    1>C:\all my\с++\Laboratory\Laboratory\Release\L4. Customizing stream operator.exe : fatal error LNK1169: one or more multiply defined symbols found
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Correct solution:
    Put to Point.cpp

    Code:
    //----------- Implementation of GLOBAL Ostream << Operator  -----------//
    
    	std::ostream& operator << (std::ostream& out, Point const& ObjPoint)
    {
      
    
    	return out << "[" << ObjPoint.x << "," << ObjPoint.y << "]" ; 
    	
    }
    
    		std::ostream& operator << (std::ostream& out, Point const *objPoint)
    {
      
    	return out << "[" << objPoint->x << "," << objPoint->y << "]" ; 
    	
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center