Unable to Construct ifstream Object

I'm trying to declare an ifstream intended to be opened upon a function call (I enter a filename into the program and then the ifstream opens), but every time I get a "use of deleted function" error. Could someone explain or help?
Can you post your code so we can see what's going on please?
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
int main()
{
    ArrayList<string>* filter;
    ArrayList<Star> starDB = ArrayList<Star>();
    ArrayList<Planet> pdb = ArrayList<Planet>();

    string filename = string();
    ifstream inf; //ifstream in question
    char inp;
    float inum = -1.0;
    char sortedBy = '0';

    cout << "Welcome to ExoPlanIt. Please enter the name of a planetary system file to upload it." << endl;
    cin >> filename;

    starDB.append(addSystem(filename, inf, filter));
    pdb += starDB[starDB.getSize() - 1].getPlanets();

    while(true)
    {
    
        /.../

        else if(inp == 'A')
        {
	    try
	    {
	        cin >> skipws >> filename;
		starDB.append(addSystem(filename, inf, filter)); //addSystem is the function that opens the isftream
	        pdb += starDB[starDB.getSize() - 1].getPlanets();
		cin >> skipws >> inp;
	    }
	    catch(ios_base::failure& ff)
	    {
	        cout << "Error: File could not be opened." << endl;
	    }
        }

        /.../
    }
}


I should mention that I get a similar error (same type, different details) when I try to instantiate the ifstream in main with a default constructor (ifstream()).
Last edited on
Pass the stream by reference (streams are not CopyConstructible).

1
2
//... addSystem(  ... , std::ifstream, .... ) ;
... addSystem(  ... , std::ifstream&, .... ) ;
But... I do pass it by reference.

The code that's causing an error specifically is
 
inp = ifstream(name, ios_base::in);

Where inp is an ifstream& passed from the function call.
That should work with a conforming implementation of the standard library; though a file stream is not CopyAssignable, it is MoveAssignable

Use this construct to open the file; it should work everywhere: inp.open( name, std::ios_base::in ) ;
Last edited on
Topic archived. No new replies allowed.