简体   繁体   中英

iostream functions giving me error C2248

I am writing a very basic program that takes the contents of two files and adds them together in a third file. My code is as follows:

#include "std_lib_facilities.h"

string filea, fileb,
    contenta, contentb;

string getfile()
{
string filename;
bool checkname = true;
while (checkname)
{
cout << "Please enter the name of a file you wish to concatenate: ";
cin >> filename;
ifstream firstfile(filename.c_str());
if (firstfile.is_open())
{
    cout << filename << " opened successfully.\n";
    checkname = false;
}
else cout << "Cannot open " << filename << endl;
}
return filename;
}

string readfile(ifstream ifs)
{
string content;
while (!ifs.eof())
{
    string x;
    getline(ifs, x);
    content.append(x);
}
return content;
}

int main()
{
ifstream firstfile(getfile().c_str());
ifstream secondfile(getfile().c_str());
ofstream newfile("Concatenated.txt");

newfile << readfile(firstfile) << endl << readfile(secondfile);
system("PAUSE");
firstfile.close();
secondfile.close();
newfile.close();
return 0;
}

When I try to compile this code, however, I get this error:

 error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

I don't know what is causing this error, though I suspect it has something to do with the functions I have made, as I did not have this error before creating the functions.

Any help is greatly appreciated, thanks in advance for any replies.

You've defined your readfile to pass the stream by value, which would require access to the stream's copy constructor. That's private, because you're not supposed to copy streams. Pass the stream by reference instead (then rewrite readfile to fix the loop, since while (!xxx.eof()) is broken).

make it reference

string readfile(ifstream & ifs) {
//                       ^

because fstream is not copyable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM