C++.如何从函数写入文件

Inf*_*eus 0 c++ c++11 visual-studio-2012

我有功能必须在文件中写一些东西.

我这样试试:

int main() {
    std::ofstream fout;
    fout.open("OUTPUT.TXT");
    i = searchLexemes(input, i, 1, fout);
}
Run Code Online (Sandbox Code Playgroud)

searchLexemes以这种方式定义:

int searchLexemes(std::string value, int i, int type, std::ofstream fout);
Run Code Online (Sandbox Code Playgroud)

如果我像在main()中那样调用searchLexemes,Visual Studio会给出错误:

IntelliSense:"std :: basic_ofstream <_Elem,_Traits> :: basic_ofstream(const std :: basic_ofstream <_Elem,_Traits> :: _ Myt&_Right)[с_Elem= char,_Traits = std :: char_traits]"(объявленовстроке1034 из"C:\ Program Files\Microsoft Visual Studio 11.0\VC\include\fstream")недоступноc:\ Users\Badina\Documents\Visual Studio 2012\Projects\PLT lab1\PLT lab1 \Исходныйкод.cpp191 33 PLT lab1

我正在使用俄罗斯版的VS 2012,但我想问题必须清楚.

小智 7

使用reference声明你的功能,如下所示:

int searchLexemes(const std::string& value, int i, int type, std::ofstream& fout);
Run Code Online (Sandbox Code Playgroud)

您遇到此错误的原因是因为std::ofstream没有公共复制构造函数(例如:) ofstream(const ofstream& rhs),或者delete在C++ 11中明确标记为d.