编译错误:ifstream :: open只接受引号""中的字符串值而不接受字符串变量

Jas*_*Kim 5 c++ fstream g++ ifstream

open函数是否对传入什么类型的字符串值有某种限制?

ifstream file;
string filename = "output.txt";
file.open(filename);
Run Code Online (Sandbox Code Playgroud)

我试图用字符串变量传递一个字符串值,但是当它尝试编译时,结果是......

agent.cpp:2:20: error: ofstream: No such file or directory
agent.cpp: In function ‘std::string readingline(std::string)’:
agent.cpp:11: error: aggregate ‘std::ifstream file’ has incomplete type and cannot be     defined
agent.cpp: In function ‘int main()’:
agent.cpp:44: error: aggregate ‘std::ofstream writefile’ has incomplete type and cannot be defined
Run Code Online (Sandbox Code Playgroud)

另一方面,当我只是在"filename.txt"之类的引号中传递字符串值时,它编译正常并运行正常.

ifstream file;
file.open("output.txt");
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

有没有办法解决这个问题?

Xeo*_*Xeo 5

可悲的是,这是怎样的构造和openstd::(i|o)fstream是由标准定义.使用file.open(filename.c_str()).

一些标准库提供了一个扩展,允许std::string作为参数,例如Visual Studio.