简体   繁体   English

C ++读取字符串,后跟两个双精度

[英]C++ Reading in a string followed by two doubles

I am getting the following error when trying to read in to a string then two doubles from a file: 尝试读取字符串然后从文件中读取两次时,出现以下错误:

Error: no operator ">>" matches these operands operand types are: std::ifstream >> std::string 错误:没有运算符“ >>”与这些操作数匹配,操作数类型为:std :: ifstream >> std :: string

For the following code: 对于以下代码:

std::string name;
double mass(0), radius(0), gravity(0);
std::ifstream inFile;
inFile.open("solSystem.txt", std::ios::app);
inCheck(inFile);
while (inFile >> name >> radius >> mass)
{
    someFunction(name, radius, mass);
}
inFile.close();

I have used the same code before (std::ifstream >> std::string) without problem however in that instance the input file had only characters. 我之前使用过相同的代码(std::ifstream >> std::string) ,但是在这种情况下,输入文件只有字符。

This is the input file: 这是输入文件:

Sun       6.96e+08    1.989e+30
Mercury   2.44e+06    3.285e+23
Venus     6.052e+06   4.867e+24
Earth     6.371e+06   5.972e+24
Mars      3.39e+06    6.39e+23
Jupiter   6.9911e+07  1.898e+27
Saturn    5.8232e+07  5.683e+26
Uranus    2.5362e+07  8.681e+25
Neptune   2.4622e+07  1.024e+26
Pluto     1.186e+06   1.309e+22

Before you answer, I am looking for a solution to this yes, but I also want to know why I am getting this error that I might avoid it in the future. 在您回答之前,我正在寻找解决方案,但是我也想知道为什么会收到此错误,以后我可能会避免。

So anyone who stumbles upon this issue as well can find a resolution. 因此,任何在此问题上迷迷糊糊的人都可以找到解决方案。

  1. Check to make sure all needed header files are included 检查以确保包括所有需要的头文件
  2. Check any custom headers to see if you include other headers in them.... this can cause issues like it did for me 检查所有自定义标题,看看是否在其中包含其他标题...。这可能会导致像我这样的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM