繁体   English   中英

iostream函数给我错误C2248

[英]iostream functions giving me error C2248

我正在编写一个非常基本的程序,该程序需要两个文件的内容并将它们添加到第三个文件中。 我的代码如下:

#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;
}

但是,当我尝试编译此代码时,出现以下错误:

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

我不知道是什么导致了此错误,尽管我怀疑它与我所做的功能有关,因为在创建功能之前我没有此错误。

非常感谢您的帮助,在此先感谢您的答复。

您已经定义了readfile来按值传递流,这将需要访问流的副本构造函数。 这是私人的,因为您不应该复制流。 而是通过引用传递流(然后重写readfile以修复循环,因为while (!xxx.eof())损坏了)。

使其参考

string readfile(ifstream & ifs) {
//                       ^

因为fstream是不可复制的。

暂无
暂无

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

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