我不太明白这个函数是如何工作的。
我写了一个简单的程序,用 getline() 读取一行。
例如:
ifstream in;
in.open("example.txt");
string line;
getline(in, line);
cout << line << endl;
当我尝试运行这个程序时,我收到了这样的错误消息。
`assign1_2.cpp:33:20: error: cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int atoi(const char*)'
我根本不明白这里出了什么问题。请帮忙!。我是 C++ 的新手。
最佳答案
您没有显示错误代码,但错误表明您尝试使用类型为 std::string
的参数调用 atoi
。 atoi
接受一个 C 字符串 (man atoi
),所以你需要像这样调用它:
atoi( line.c_str() );
关于C++ getline() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8562620/