【发布时间】:2015-08-06 15:29:48
【问题描述】:
我正在尝试为 c++ 编译器(代码块)不断向我显示的错误找到解决方案,我在网上搜索了答案,但似乎都没有帮助。 我有一个 txt 文件,其中有数字,每个数字都写在一行中。 我想要的是读取每个数字(逐行)并将其从字符串转换为浮点数后将其存储在变量中。 这是我的代码
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
float z;
int j=0;
stringstream s;
const ifstream fich1;
fich1.open("test.txt",ios::in);
if(!fich1)
{
cerr<<"could not open the file";
EXIT_FAILURE;
};
else const string ligne1;
while((!fich1.eof())
{
if(j!=i)
{
j++;
getline(fich1,l1); // the first error is: no matching function for call to ‘getline(const ifstream&, const string&)
else if(j == i)
{
fich1>>s; // the second error is: ambiguous overload for ‘operator>>’ (operand types are ‘const ifstream {aka const std::basic_ifstream<char>}’ and ‘std::stringstream {aka std::basic_stringstream<char>}’)
z=(float)s; // I couldn't find a clear answer to convert from string to float
}
}
如果有人想询问有关代码的任何问题以使其更清晰,请提出,我正在等待您的回答以及您的问题:)
【问题讨论】:
-
正确缩进你的代码。我做了部分编辑来帮助你。
-
这篇文章需要立即编辑!代码中混有错误!
-
这是我将主要部分和文件 class.cpp 混合在一起的整个代码的摘录,只是为了告诉你我在哪里得到了错误!