This is a school assignment. I have to write a function in my program that opens a file stream and returns a heaped value to the main function. I believe the file opening function works but after it passes the ifstream pointer to the main function, I'm having trouble dereferencing it.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
char getOption();
ifstream * openInputFile();
int main()
{
char userOption;
ifstream * currentFile;
ifstream fileInUse;
do //calls the getOption function until user inputs 'q' or 'Q'
{
userOption = getOption();
if ((userOption == 'i') || (userOption == 'I'))
{
currentFile = openInputFile();
fileInUse = * currentFile;
}
}
while ((userOption != 'q') && (userOption != 'Q'));
return 0;
}
char getOption() //prompts user for input and sends it back to main function
{
//Edited out for simplicity
}
ifstream * openInputFile() //opens a file stream and returns it to main function as a heaped value
{
string fileName;
ifstream * sendFile;
cout << "Please enter a valid file name: ";
cin >> fileName;
cout << "\n\n";
ifstream newFile(fileName);
sendFile = & newFile;
return sendFile;
}
The function names are required to be like that. When I try to compile, I get the following error:
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(860): error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator =(const std::basic_istream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]