【发布时间】:2011-12-18 03:47:51
【问题描述】:
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");
为什么会这样?
有没有办法解决这个问题?
【问题讨论】:
-
您是否尝试将指针传递给 char 数组?