0

我正在尝试在 VS 2010 上用 C++ 运行一个简单的文件 I/O 处理程序。但是,当我尝试运行它时,我遇到了一个与 fstream 有关的错误。该程序相当简单,包括打开 2 个文本文件并将文本从第一个文件复制到第二个文件,并稍作改动。我很确定我的逻辑是正确的,但我不确定我做错了什么。任何帮助将非常感激。谢谢。这是代码:

#include<fstream>
#include<iostream>
#include<cstdlib>

using namespace std;

void file1(ifstream& in_stream, ofstream out_stream);

int main()
{
ifstream fin;
ofstream fout;
char name1[60];
cout<<"Enter the name of the input file: "<<endl;
cin>>name1;
fin.open(name1);
if(fin.fail())
{
    cout<<"Failed to open Input file"<<endl;
    exit(1);
}
else
{
    cout<<"Input file opened successfully"<<endl;
}
char name2[60];
cout<<"Enter the name of the output file: "<<endl;
cin>>name2;
fout.open(name2);
if(fout.fail())
{
    cout<<"Failed to open Output file"<<endl;
    exit(1);
}
else
{
    cout<<"Output file opened successfully"<<endl;
}
file1(fin, fout);
fin.close();
fout.close();
return 0;
}

void file1(ifstream& in_stream, ofstream out_stream)
{
char next;
in_stream.get(next);
while(!in_stream.eof())
{
    if(next=='A')
    {
        out_stream<<"ABC";
    }
    else
    {
        out_stream<<next;
    }
    in_stream.get(next);
}
}

这是我在 VS 2010 上遇到的确切错误:

错误 1 ​​错误 C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : 无法访问在类 'std::basic_ios<_Elem,_Traits>' c:\program files (x86)\microsoft visual studio 中声明的私有成员10.0\vc\include\fstream 1116 1 个文件

4

1 回答 1

4

out_stream正在按值传递,这意味着正在尝试调用其复制构造函数,即private:使其成为引用:

void file1(ifstream& in_stream, ofstream& out_stream);
于 2012-05-08T10:22:07.297 回答