outFile command in another member function of a class

Although I have professionally programmed in the past (Java, Coldfusion) I'm following a course on C++ at university and as a homework assignment we have to draw a diamond to screen and/or text file using a Diamond class and member functions. Both the code to draw the Diamond on screen or file is nearly the exact same, except the output command. For the screen it's just 'cout', for the file it's the handle of the output file (in my case outFile). So, I created an extra member function drawDiamond(char functie) that contains the commands to draw a diamond and that can be used by either the member function drawScreen or drawFile. Within the drawDiamond function I then test whether the diamond needs to be drawn to the screen or to a file to select either 'cout <<'*';' or 'outFile<<'*';' However outside my function drawFile the handle 'outFile' is not recognized. I have tried to solve this by adding the declaration of 'string outFile' with the private members of my Diamond class in the header file, but then I get errors on the << of the command (even though if I force to go ahead and run the program anyway, my file is made).

Can someone think of improving this?

Here's the code of the header file Diamond.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Member functies gedefinieerd in Diamond.cpp
#include <iostream>
using namespace std;

// Diamond class definiëring
class Diamond
{
public:
	// Constructor initializes side
	Diamond (double);
	void setSide(double);
	double getSide();
	double getSurface();
	double getCircumference();
	void drawScreen();
	void drawFile(string filename);
	void drawDiamond(char functie);

private:
	double diamondSide;
	string outFile;
};


Here's the code of my Diamond.cpp with the member functions
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Opdracht 2: Diamond.cpp
// 
#include <iostream>
using namespace std;

#include <cmath>
#include <fstream>

#include "Diamond.h"

//
Diamond::Diamond (double Side)
{
	setSide( Side );
}
//
void Diamond::setSide(double Side)
{
	if (Side<2.) {
		cout << "Error: zijde van de ruit kleiner dan 2 is niet mogelijk: "<< Side << endl;
		cout << "       zijde waarde ingesteld als 2." << endl;
		diamondSide=2.;
	} else diamondSide=Side;
}
//
double Diamond::getSide()
{
	return diamondSide;
}
// Berekent de oppervlakte van de diamond, die als een gekanteld vierkant wordt gezien
double Diamond::getSurface()
{
	return pow(diamondSide,2);
}
// Berekent de omtrek van de diamond
double Diamond::getCircumference()
{
	return 4*diamondSide;
}
// Tekent een holle ruit op het scherm
void Diamond::drawScreen()
{
	char border='*', spatie=' '; // tekens instellen
	int side=(int)floor(diamondSide); // casting van double naar int door de floor te nemen van een decimaal getal
	cout << endl;
	cout << "De ruit tekenen:" << endl;
	cout << endl;	
	drawDiamond('s');
	
}
void Diamond::drawFile(string filename){
	// open sequentiële file om te schrijven
	ofstream outFile(filename);

	//exit program if unable to create file
	if (!outFile)
	{
		cerr << "File could not be opened" << endl;
		exit (1);
	}
	
	//hier schrijven wat er in de file moet geschreven worden
	cout<<"De ruit in het tekstbestand Diamond.txt aan het schrijven."<<endl;
	drawDiamond('f');

	outFile.close();
	cout << "Done!"<<endl;
}
void Diamond::drawDiamond(char functie){
	char border='*', spatie=' '; // tekens instellen
	int side=(int)floor(diamondSide); // casting van double naar int door de floor te nemen van een decimaal getal

	// tekent de bovenste helft van de ruit
	for(int i=1, rechts=side, links=side; i<=side; i++, rechts++, links--){ // i = rij
		for(int j=1; j<2*side; j++){ // j = kolom
			if(j==rechts || j==links) { // tekent *
				if (functie=='s') cout <<border; 
				else outFile<<border;		
			}
			else{ // zet een spatie
				if (functie=='s') cout <<spatie; 
				else functie<<spatie;
			}
		}
		if (functie=='s') cout <<endl; 
		else outFile<<endl;
	}

	// tekent de onderste helft van de ruit
	for(int i=1, links=2, rechts=2*side-2; i<side; i++, links++, rechts--){
		for(int j=1; j<=2*side-1; j++){
			if(j==links || j==rechts){
				if (functie=='s') cout <<border; 
				else outFile<<border;	
			}
			else{ // zet een spatie
				if (functie=='s') cout <<spatie; 
				else outFile<<spatie;
			}
		}
		if (functie=='s') cout <<endl; 
		else outFile<<endl;
	}
}


And here's my test file with the main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// TestDiamond.cpp : Maakt een ruit door een opgegeven zijde 
// en berekent de oppervlakte en omtrek van de ruit


#include <iostream>
using namespace std;

#include "Diamond.h"


void main()
{
	cout << "Een ruit maken."<<endl;
	Diamond myDiamond(5.);
	double side = myDiamond.getSide();
	cout << "De zijde: " << side << endl;
	double surface = myDiamond.getSurface();
	cout << "De oppervlakte: " << surface << endl;	
	double circum = myDiamond.getCircumference();
	cout << "De omtrek: " << circum << endl;	
	myDiamond.drawScreen();
	myDiamond.drawFile("diamond.txt");
	system("pause");
}
Instead of passing a char to drawDiamond(...) you should pass an ostream &. Both cout and ofstream are derived from that.

What you're actually trying to do is using the string as a stream which failes.
Makes sense. And how do I go about that?
I've tried this:

for the member function in the cpp file
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
43
44
45
46
47
void Diamond::drawScreen()
{
	cout << endl;
	cout << "De ruit tekenen:" << endl;
	cout << endl;	
	drawDiamond(cout); //uses the member function drawDiamond for output on a screen: 's'
	
}
void Diamond::drawFile(string filename){
	// opens sequence file to write output
	ofstream outFile(filename);

	//exit program if unable to create file
	if (!outFile)
	{
		cerr << "File could not be opened" << endl;
		exit (1);
	}
	
	cout<<"De ruit in het tekstbestand Diamond.txt aan het schrijven."<<endl; 	// feedback on screen
	drawDiamond(outFile); // uses the member function drawDiamond for output in a file: 'f'

	outFile.close(); // closes the file
	cout << "Done!"<<endl; 	// feedback on screen
}
void Diamond::drawDiamond(ostream& out){
	char border='*', spatie=' '; // sets the characters used
	int side=(int)floor(diamondSide); // casting of the double to int by taking the floor of a double

	// draws the upper half of the diamond
	for(int i=1, rechts=side, links=side; i<=side; i++, rechts++, links--){ // i = row
		for(int j=1; j<2*side; j++){ // j = column
			if(j==rechts || j==links) out <<border; // draws * either on screen or file
			else out <<spatie; // draws a space on screen or file
		}
		out <<endl;  //goes to the next line on screen or file
	}

	// draws the lower half of the diamond
	for(int i=1, links=2, rechts=2*side-2; i<side; i++, links++, rechts--){
		for(int j=1; j<=2*side-1; j++){
			if(j==links || j==rechts) out <<border; 
			else out <<spatie; // zet een spatie
		}
		out <<endl; 
	}
}


and this for the header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Diamond class definition
class Diamond
{
public:
	// Constructor initializes side
	Diamond (double);
	void setSide(double); 
	double getSide();
	double getSurface();
	double getCircumference();
	void drawScreen();
	void drawFile(string filename);
	void drawDiamond(ostream& out);

private:
	double diamondSide;
};
Thank you very much! That solved it all.
Topic archived. No new replies allowed.