为什么 i(f)流无法读取双精度
Why i(f)stream can't read a double
我正在编写一个程序,需要实现的一件事是过载>>
操作员一次以一次读取点。我尝试了什么:
std::istream& operator >>(std::istream& is, const Point& point)
{
double temp;
is >> temp;
point.setx(temp);
is.ignore(3,';');
is >> temp;
point.sety(temp);
is.ignore(3,';');
}
它某种程度上未能在VS2019中编译(如果很重要的话(,并说没有操作员>>
接受std::istream
&double
作为参数。任何类型的temp
仍然存在问题。它是int
或char*
,不取决于我是从istream
还是从ifstream
阅读。
这种行为的原因是什么?
也许您忘记了#include <iostream>
?没有这些,您希望像您所引用的那样收到一条错误消息。
解决此问题后,我希望看到其他几个警告/错误。首先,即使operator>>
似乎正在修改已通过的Point
(这是预期的(,您也可以通过point
通过CC_11。您可能要删除const
。
第二,您已声明您的operator>>
返回std::istream &
,但未能返回任何内容。您可能想在结束之前添加return is;
。
此代码编译:
#include <iostream>
struct Point {
double x;
double y;
void setx(double x_) { x = x_; }
void sety(double y_) { y = y_; }
};
std::istream& operator >>(std::istream& is, Point& point)
{
double temp;
is >> temp;
point.setx(temp);
is.ignore(3,';');
is >> temp;
point.sety(temp);
is.ignore(3,';');
return is;
}
相关文章:
- 正在将csv文件读取为双精度矢量
- 为什么 Clang std::ostream 写一个 std::istream 无法读取的双精度?
- 从二进制流中读取时,将双精度变量的地址转换为 char* 意味着什么?
- 如何从char16_t字符串文字中读取双精度?
- 从.txt文件读取到C++中的双精度向量
- 警告:左移计数 >= 在 C++ 中将字节流读取为双精度变量时的类型宽度
- 我想读取文本文件中的所有内容(但文本文件中只有一个双精度值),转换为双精度值并返回值
- 为什么当我使用双精度时,Qt<->Matlab 正确写入和读取我的字节,但存储 uint32 的字节不正确?
- 为什么 i(f)流无法读取双精度
- 如何从二进制文件中读取双精度并将其保存在向量中
- 使用 sscanf 读取双精度时忽略"E"
- 从十六进制数组读取双精度到向量
- 如何使用 istringstream 从 c++ 中的字符串中读取双精度
- c++从文本文件读取双精度+值
- 如何从文件中读取双精度
- 如何在Linux中读取双精度体时测试行尾
- 从.txt文件中读取双精度值到双精度数组c++
- 从二进制文件读取双精度(到类中)-小数不显示
- c++从二进制文件中写入和读取双精度矩阵
- 从文件中读取双精度对象对(c++)