fstream is inaccessible

Ok, so my problem is that for some reason my fstream (named "myfile") is inaccessible in my cpp file. I will show you the code for the involved methods and the imports I have 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
#include <fstream>
#include "EmployeeBST.h"
#include <string.h>
#include <sstream>
#include <iomanip>
#include "TreeNode.h"
#include <iostream>


void EmployeeBST::save(){
	fstream myfile;
	myfile.open ("example.txt");
	saveHelper( r, myfile);
	myfile.close();
}
void EmployeeBST::saveHelper( TreeNode* root, fstream myfile){	
	
	if(r != NULL){
		myfile<<"hi"<<endl;
		myfile << root->e.getLastName() << " " << root->e.getFirstName() << " " << root->e.getEmployeeID() << "\r\n";
		saveHelper( root->getLeft(), myfile);
		saveHelper( root->getRight(), myfile);
	}
	
}




and here is my h 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
#ifndef EMPLOYEEBST_H
#define	EMPLOYEEBST_H
#include "TreeNode.h"
#include "Employee.h"


class EmployeeBST{

public:
    //constructor
	EmployeeBST();
	~EmployeeBST();
	void deleteTree(TreeNode *root);
	void insert(Employee e);
	//void insertHelper(Employee e,TreeNode *pre, TreeNode *root,bool left);
	void deleteEmployee(int ID);
	void deleteHelper(int ID, TreeNode *root);
	void search(int ID);
	void searchHelper(int ID, TreeNode *root);
	void save();
	void saveHelper( TreeNode* root, fstream myfile);
	void quit(EmployeeBST *bst);

	TreeNode *r;
	//TreeNode *curr;
	//TreeNode *par;
private:
};
#endif 



Can anyone help me?
Also, the errors are at lines 13(the saveHelper call), lines 21, and 22.
fstream is part of the std namespace so it should be std::fstream
That's something I forgot to include. I have "using namespace std" at the top of my cpp file.
std::fstream doesn't have a copy constructor, so you'll have to pass by reference instead:
void saveHelper( TreeNode* root, fstream& myfile);
Ok, that got rid of the accessibility issues, but my program is still crashing when I try to save the file. Is anything obviously off about this code? I'm new to c++, so I might be missing something basic.
Scratch that. The program compiles, but example.txt isn't being created. Ideas?
that may be because of the default open mode of fstream (in|out)
use std::ofstream
Topic archived. No new replies allowed.