【问题标题】:<< Operator Rewrite to cout int and double values<< 运算符重写为 cout int 和 double 值
【发布时间】:2010-12-05 23:54:02
【问题描述】:

我需要重写

我想我已经包含了所有必要的部分。提前致谢。

struct Reading {
    int hour;
    double temperature;
    Reading(int h, double t): hour(h), temperature(t) { }
    bool operator<(const Reading &r) const;
};

========

ostream& operator<<(ostream& ost, const Reading &r)
{
    // unsure what to enter here

    return ost;
}

========

vector<Reading> get_temps()
{
// stub version                                                                 
    cout << "Please enter name of input file name: ";
    string name;
    cin >> name;
    ifstream ist(name.c_str());
    if(!ist) error("can't open input file ", name);

    vector<Reading> temps;
    int hour;
    double temperature;
    while (ist >> hour >> temperature){
        if (hour <0 || 23 <hour) error("hour out of range");
        temps.push_back( Reading(hour,temperature));
    }

}

【问题讨论】:

  • 你有什么问题?您是要我们为您编写函数吗?
  • 重复:stackoverflow.com/questions/4362077/… 没有必要问两个相同/相似的问题。
  • 你只是在重复你之前的问题:stackoverflow.com/questions/4362077/…
  • 小时重要还是仅仅意味着温度?
  • 请不要在标题中添加标签,也不要一遍又一遍地问同一个问题。在答案上使用 cmets 与他们的作者互动并编辑您的问题以添加更多详细信息。

标签: c++ iostream ostream


【解决方案1】:

例如这样:

bool operator <(Reading const& left, Reading const& right)
{
    return left.temperature < right.temperature;
}

并且它应该是一个全局函数(或与Reading 在同一个命名空间中),而不是成员或Reading,如果您要拥有任何受保护或私有成员,则应将其声明为friend。可以这样做:

struct Reading {
    int hour;
    double temperature;
    Reading(int h, double t): hour(h), temperature(t) { }

    friend bool operator <(Reading const& left, Reading const& right);
};

【讨论】:

  • 确保运算符定义位于结构所在的同一个命名空间中,以便在依赖于参数的查找过程中可以找到它。
  • 没想到你可以拥有struct 的私人成员 - 不是必须是class 吗?在这种情况下,是的,使用friend 以便可以访问私有/受保护成员。
  • 出于好奇,您能否详细说明为什么说它不应该是成员函数?
  • @Will:除了结构默认为public 和类默认为private 之外,C++ 中的结构和类之间绝对没有区别。但是您可以在结构中拥有私有成员,就像拥有类的公共成员一样容易。
  • @jalf:谢谢。我不确定;我不记得曾经将struct 用于具有私有数据的类型。对我来说,这始终是类的功能,不过,正如您所说,两者是相同的,保存其成员的默认可见性级别。
【解决方案2】:

你可能想要类似的东西

ost << r.hour << ' ' << r.temperature;

不过,这是非常简单的事情,如果没有意义,您应该真正与某人交谈或买一本书。

如果它仍然没有意义或者你不能被打扰,考虑选择另一个爱好/职业。

【讨论】:

    【解决方案3】:

    IIRC,您可以通过以下两种方式之一来实现...

    // overload operator<
    bool operator< ( const Reading & lhs, const Reading & rhs )
    {
      return lhs.temperature < rhs.temperature;
    }
    

    或者,您可以将运算符添加到您的结构中...

    struct Reading {
      int hour;
      double temperature;
      Reading ( int h, double t ) : hour ( h ), temperature ( t ) { }
      bool operator< ( const Reading & other ) { return temperature < other.temperature; }
    }
    

    【讨论】:

      【解决方案4】:

      在 operator

      【讨论】:

        【解决方案5】:
        r.hour()
        r.temperature()
        

        您已将hourtemperature 声明为Reading 的成员字段,而不是成员方法。因此它们只是r.hourr.temperature(不是())。

        【讨论】:

          【解决方案6】:

          由于小时和温度是变量而不是函数,因此只需从 operator&lt;&lt; 函数中删除尾随的 ()

          【讨论】:

            【解决方案7】:

            您可以在 c++ 中重载这样的运算符。

            struct Reading {
                 int hour;
                 double temperature;
                 Reading(int h, double t): hour(h), temperature(t) { }
                 bool operator<(struct Reading &other) {
                     //do your comparisons between this and other and return a value
                 }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2014-02-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-07-30
              相关资源
              最近更新 更多