简体   繁体   English

iostream函数给我错误C2248

[英]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. 您已经定义了readfile来按值传递流,这将需要访问流的副本构造函数。 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). 而是通过引用传递流(然后重写readfile以修复循环,因为while (!xxx.eof())损坏了)。

make it reference 使其参考

string readfile(ifstream & ifs) {
//                       ^

because fstream is not copyable. 因为fstream是不可复制的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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