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

时间:2011-12-18 03:47:52

标签: c++ g++ fstream ifstream

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

ifstream file;
string filename = "output.txt";
file.open(filename);

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

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

另一方面,当我只用“filename.txt”这样的引号传递一个字符串值时,它编译得很好并且运行正常。

ifstream file;
file.open("output.txt");

为什么会这样?

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

0 个答案:

没有答案