【发布时间】:2009-07-21 13:15:37
【问题描述】:
我正在学习 C++,当我尝试在 ifstream 方法中使用 String 时遇到了一些麻烦,如下所示:
string filename;
cout << "Enter the name of the file: ";
cin >> filename;
ifstream file ( filename );
这里是完整的代码:
// obtaining file size
#include <iostream>
#include <fstream>
using namespace std;
int main ( int argc, char** argv )
{
string file;
long begin,end;
cout << "Enter the name of the file: ";
cin >> file;
ifstream myfile ( file );
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "File size is: " << (end-begin) << " Bytes.\n";
return 0;
}
这里是Eclipse的错误,方法前的x:
no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)'
但是当我尝试在 Eclipse 中编译时,它会在方法前放置一个 x,这表示语法错误,但语法有什么问题?谢谢!
【问题讨论】:
-
您能否提供有关您遇到的错误的更多详细信息?或者,也许您可以发布一个完整的示例......
-
也许 fstream 不包括在内?请提供完整代码
-
您是否包含了正确的标题?
标签: c++ string file-io input ifstream