簡體   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