重载运算符有帮助吗?

发布于 2024-12-11 12:17:08 字数 1642 浏览 0 评论 0原文

一如既往,我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冧九 2024-12-18 12:17:08

您必须遵守标准流习惯用法:每个流都可以隐式转换为 bool (或 void 指针),以允许像 if (in >> n) 这样的检查来查看操作是否成功。因此,首先您必须确保您的操作符符合这一点(通过确保如果提取成功则流是“良好的”)。

其次,当您编写像 while (in >> x) { /*...*/ } 这样的循环时,那么在循环终止后,您已经知道您的流不再良好。因此,在返回之前,您必须对其调用 clear()

也许是这样的:

std::istream& operator>> (std::istream &in, sample &sample)
{
  char   c;
  int    n;
  double d;
  std::vector<double> vd;

  if (!(in >> c)) { return in; }                             // input error
  if (c != '>')   { in.setstate(std::ios::bad); return in; } // format error

  if (!(in >> n)) { return in; }                             // input error

  if (!(in >> c)) { return in; }                             // input error
  if (c != ':')   { in.setstate(std::ios::bad); return in; } // format error

  while (in >> d)
  {
    vd.push_back(d);
  }

  in.clear();

  if (!(in >> c)) { return in; }                             // input error
  if (c != '>')   { in.setstate(std::ios::bad); return in; } // format error

  state.n = n;
  state.data.swap(vd);

  return in;
}

请注意,如果整个输入操作成功,我们只会修改 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 call clear() on it before returning it.

Maybe something like this:

std::istream& operator>> (std::istream &in, sample &sample)
{
  char   c;
  int    n;
  double d;
  std::vector<double> vd;

  if (!(in >> c)) { return in; }                             // input error
  if (c != '>')   { in.setstate(std::ios::bad); return in; } // format error

  if (!(in >> n)) { return in; }                             // input error

  if (!(in >> c)) { return in; }                             // input error
  if (c != ':')   { in.setstate(std::ios::bad); return in; } // format error

  while (in >> d)
  {
    vd.push_back(d);
  }

  in.clear();

  if (!(in >> c)) { return in; }                             // input error
  if (c != '>')   { in.setstate(std::ios::bad); return in; } // format error

  state.n = n;
  state.data.swap(vd);

  return in;
}

Note that we only modify the sample object if the entire input operation succeeded.

走走停停 2024-12-18 12:17:08
cout << in << " " << number;

你可能是指

cout << " " << number;

什么

cout << in << " " << number;

you probably meant

cout << " " << number;

or something

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文