我正在尝试将从文件中读取的字符串转换为 int 值,以便可以将其存储在整数变量中。这就是我的代码的样子:
ifstream sin;
sin.open("movie_output.txt");
string line;
getline(sin,line);
myMovie.setYear(atoi(line));
在这里,setYear 是 Movie 类中的一个变元(myMovie 是 Movie 类的一个对象),如下所示:
void Movie::setYear(unsigned int year)
{
year_ = year;
}
当我运行代码时,出现以下错误:
error C2664: 'atoi' : cannot convert parameter 1 from 'std::string' to 'const char *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
而不是使用std::getline(std::string&, std::istream&)
,为什么不在文件上使用流提取运算符呢?
ifstream sin;
sin.open("movie_output.txt");
unsigned int year = 0;
sin >> year;
myMovie.setYear(year);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)