C ++读取一个字符串后跟两个双打
C++ Reading in a string followed by two doubles
我在尝试读入一个字符串然后从一个文件中读入两个双打时收到以下错误:
Error: no operator ">>" matches these operands
operand types are: std::ifstream >> std::string
对于以下代码:
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();
我在 (std::ifstream >> std::string)
之前使用了相同的代码,没有问题,但是在那个例子中,输入文件只有字符。
这是输入文件:
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
在你回答之前,我正在寻找解决这个问题的方法,但我也想知道为什么我会收到这个错误,以便我将来可以避免它。
所以任何偶然发现这个问题的人都可以找到解决方案。
- 检查以确保包含所有需要的 header 文件
- 检查任何自定义 header,看看您是否在其中包含其他 header...。这可能会导致像我这样的问题
我在尝试读入一个字符串然后从一个文件中读入两个双打时收到以下错误:
Error: no operator ">>" matches these operands operand types are: std::ifstream >> std::string
对于以下代码:
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();
我在 (std::ifstream >> std::string)
之前使用了相同的代码,没有问题,但是在那个例子中,输入文件只有字符。
这是输入文件:
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
在你回答之前,我正在寻找解决这个问题的方法,但我也想知道为什么我会收到这个错误,以便我将来可以避免它。
所以任何偶然发现这个问题的人都可以找到解决方案。
- 检查以确保包含所有需要的 header 文件
- 检查任何自定义 header,看看您是否在其中包含其他 header...。这可能会导致像我这样的问题