gpt4 book ai didi

c++ - iostream 函数给我错误 C2248

转载 作者:搜寻专家 更新时间:2023-10-31 00:39:17 25 4
gpt4 key购买 nike

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

#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 定义为按值传递流,这需要访问流的复制构造函数。那是私有(private)的,因为您不应该复制流。改为通过引用传递流(然后重写 readfile 以修复循环,因为 while (!xxx.eof()) 已损坏)。

关于c++ - iostream 函数给我错误 C2248,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16411177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com