overloading <<

I'm trying to overload ofstream << to print to a file. I have 3 files. One header, and 2 .cpp. I'm opening my file in my main.cpp but I only get a blank text file when I try to write from another .cpp.

Here's how I opened it in main:

 
  ofstream FileOut ("TestShapeFileData.txt"); 


Here's what i'm trying to print out in another .cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Print Rectangle coordinates
ofstream & poly1::operator<<(ofstream &FileOut, Rectangle &PointRectangle)
{
	Matrix Temp1(2,1);
	Matrix Temp2(2,1);
	Matrix Temp3(2,1);
	Matrix Temp4(2,1);

	Temp1=PointRectangle.GetVertex1();
	Temp2=PointRectangle.GetVertex2();	
	Temp3=PointRectangle.GetVertex3();	
	Temp4=PointRectangle.GetVertex4();

	FileOut<<"__________________________________________________________________________"<<endl; 
	FileOut<<"Vertex\t1\t\t2\t\t3\t\t4"<<endl; 
	FileOut<<"__________________________________________________________________________"<<endl; 
	FileOut<<"\t"<<Temp1.GetMatrixData(1,1)<<"\t\t"<<Temp2.GetMatrixData(1,1)<<"\t\t"<<Temp3.GetMatrixData(1,1)<<"\t\t"<<Temp4.GetMatrixData(1,1)<<endl; 
	FileOut<<"\t"<<Temp1.GetMatrixData(2,1)<<"\t\t"<<Temp2.GetMatrixData(2,1)<<"\t\t"<<Temp3.GetMatrixData(2,1)<<"\t\t"<<Temp4.GetMatrixData(2,1)<<endl; 
	FileOut<<"__________________________________________________________________________"<<endl;  

	return FileOut;
}


Here's how I overloaded in header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Triangle: public Shapes							
	{
		friend ofstream & operator<<(ofstream &FileOut, Triangle &PointTriangle); 	
	private:																											
		double x, y;											
		Matrix Vertex1, Vertex2, Vertex3;		
	public:		
		double InputX, InputY, GetX(), GetY(); 												
		void Translate(double i, double j);					
		void Rescale(double i, double j);					
		void Rotate(double Angle);									
		void GetInfo();	
		Matrix GetVertex1(), GetVertex2(), GetVertex3();
		Matrix Va, Vb, Vc;									

		Triangle(){x=y=0;}	
		Triangle(double InputX, double InputY)						
		{
			x=InputX;
			y=InputY;

			Matrix Va(2,1);	
			Matrix Vb(2,1);
			Matrix Vc(2,1);

			Va.SetMatrixData(1,1,0);								
			Va.SetMatrixData(2,1,0);  
			Vb.SetMatrixData(1,1,x); 
			Vb.SetMatrixData(2,1,0);
			Vc.SetMatrixData(1,1,(x/2));
			Vc.SetMatrixData(2,1,y);

			Vertex1.SetRowsColumns(2,1);
			Vertex2.SetRowsColumns(2,1);
			Vertex3.SetRowsColumns(2,1);

			Vertex1=Va;			
			Vertex2=Vb;	 			
			Vertex3=Vc;
		}
		~Triangle(){cout<<"Triangle data removed"<<endl;}											
	};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	};



	

	ofstream & operator<<(ofstream &FileOut, Triangle &PointTriangle);
	ofstream & operator<<(ofstream &FileOut, Rectangle &PointRectangle);
	ofstream & operator<<(ofstream &FileOut, Pentagon &PointPentagon);
	ofstream & operator<<(ofstream &FileOut, Hexagon &PointHexagon);


	ofstream & operator<<(ofstream &FileOut, Triangle &PointTriangle);
}

#endif 
Last edited on
Can you show your code in main where you write "FileOut << SomeShape << AnotherShape;" ?
Topic archived. No new replies allowed.