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_HPPCode:#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 } */




Reply With Quote