重载运算符有帮助吗?
一如既往,我对 C++ 相当陌生,而且我也不完全熟悉术语,所以我为提前听起来含糊不清而道歉!
我的问题是我很难理解为什么我的 while 循环似乎停止了重载运算符函数中的其余方法;
#include "sample.h"
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
sample::sample(vector<double> doubles){}
sample::sample() {}
ostream& operator<< (ostream &out, sample &sample)
{
out << "<" << sample.n << ":";
return out;
}
istream& operator>> (istream &in, sample &sample)
{
char firstChar;
in >> firstChar;
if(firstChar != '<'){
cout << "You've not entered the data in a valid format,please try again!1 \n";
exit(1);
}
int n;
in >> n;
sample.n = n;
char nextChar;
in >> nextChar;
if(nextChar != ':'){
cout << "You've not entered the data in a valid format,please try again!2 \n";
exit(1);
}
vector<double> doubles;
double number;
while (in >> number){
doubles.push_back(number);
cout << in << " " << number;
}
in >> lastChar;
return in;
}
int main(void)
{
sample s;
while (cin >> s){
cout << s << "\n";
}
if (cin.bad())
cerr << "\nBad input\n\n";
return 0;
}
我的输入是这样的;
<6:10.3 50 69.9>
我试图将 ':' 之后的所有双精度数放入向量中,如果它们是整数但一次是 '.',我可以这样做。进入后似乎就停止了。
如果我只输入整数,它似乎也会在 while(in >> number)
完成查找所有数字后停止,这很好,但是 cout<<<我的主函数中的 /code> 命令似乎不起作用!
我哪里出错了?
as ever I'm fairly new to C++ and I'm not exactly up with the lingo yet either so I apologize for sounding vague in advance!
My problem is I'm struggling to see why my while loop seems to stop the rest of my methods in my overloaded operator function;
#include "sample.h"
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
sample::sample(vector<double> doubles){}
sample::sample() {}
ostream& operator<< (ostream &out, sample &sample)
{
out << "<" << sample.n << ":";
return out;
}
istream& operator>> (istream &in, sample &sample)
{
char firstChar;
in >> firstChar;
if(firstChar != '<'){
cout << "You've not entered the data in a valid format,please try again!1 \n";
exit(1);
}
int n;
in >> n;
sample.n = n;
char nextChar;
in >> nextChar;
if(nextChar != ':'){
cout << "You've not entered the data in a valid format,please try again!2 \n";
exit(1);
}
vector<double> doubles;
double number;
while (in >> number){
doubles.push_back(number);
cout << in << " " << number;
}
in >> lastChar;
return in;
}
int main(void)
{
sample s;
while (cin >> s){
cout << s << "\n";
}
if (cin.bad())
cerr << "\nBad input\n\n";
return 0;
}
My input would be something like;
<6: 10.3 50 69.9 >
I'm trying to get all the doubles after the ':' into a vector, which I can do if they're ints but once a '.' is entered it seems to stop.
If I only put integers in, it also seems to stop after the while(in >> number)
has finished finding all the numbers, which is fine but the cout<<
command in my main function doesn't seem to work!
Where have I gone wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须遵守标准流习惯用法:每个流都可以隐式转换为 bool (或 void 指针),以允许像
if (in >> n)
这样的检查来查看操作是否成功。因此,首先您必须确保您的操作符符合这一点(通过确保如果提取成功则流是“良好的”)。其次,当您编写像
while (in >> x) { /*...*/ }
这样的循环时,那么在循环终止后,您已经知道您的流不再良好。因此,在返回之前,您必须对其调用clear()
。也许是这样的:
请注意,如果整个输入操作成功,我们只会修改
sample
对象。You have to obey the standard stream idioms: every stream is implicitly convertible to a bool (or void pointer) to allow a check like
if (in >> n)
to see if the operation succeeded. So first of all you have to make sure that your operator conforms to this (by ensuring that the stream is "good" if the extraction succeeded).Second, when you write a loop like
while (in >> x) { /*...*/ }
, then after the loop terminates, you already know that your stream is no longer good. So you'll have to callclear()
on it before returning it.Maybe something like this:
Note that we only modify the
sample
object if the entire input operation succeeded.你可能是指
什么
you probably meant
or something