我正在学习 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,这表示语法错误,但语法有什么问题?谢谢!