ifstream参数 - C++

fgf*_*jhm 1 c++ ifstream

我宣布:std::string input_file="1.txt"; 然后我试着做这个命令:

static ifstream myfile (input_file);
Run Code Online (Sandbox Code Playgroud)

我得到错误:没有匹配函数调用: std::basic_ifstream<char>::basic_ifstream(std::string&)

Mag*_*off 6

尝试:

static ifstream myfile(input_file.c_str());
Run Code Online (Sandbox Code Playgroud)

由于某种原因,ifstream构造函数不接受std::string.

  • 请注意,在C++ 11中,所有文件流**现在都有一个构造函数重载,它带有`std :: string const&`. (6认同)