Search code examples
c++stringvisual-studiofilestreamifstream

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

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.

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.


Solution

  • 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