iostream函数给出了错误C2248

时间:2013-05-07 04:25:10

标签: c++ iostream

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

#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>'

我不知道是什么导致了这个错误,虽然我怀疑它与我所做的功能有关,因为我在创建函数之前没有这个错误。

非常感谢任何帮助,感谢任何回复。

2 个答案:

答案 0 :(得分:3)

您已定义readfile按值传递流,这需要访问流的复制构造函数。那是私有的,因为你不应该复制流。通过引用传递流(然后重写readfile以修复循环,因为while (!xxx.eof())已被破坏)。

答案 1 :(得分:2)

使其成为参考

string readfile(ifstream & ifs) {
//                       ^

因为fstream不可复制。

相关问题