尽管我对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<<
命令似乎并不工作!
我哪里出错了?
感谢Kerrek,我有一个更好的理解,但混淆清楚的方法实际上做什么?!当你调用一个清除函数时,我试图从数据的数字部分到最后一个字符? – r0bb077
它根本不“走”; 'clear'只是重置错误标志,以便完全执行下一个提取操作(不会在错误的流上执行提取操作)。 –
真棒,应该真的假设!再次感谢您的帮助! – r0bb077