【问题标题】:map, class, member function映射、类、成员函数
【发布时间】:2015-11-02 02:46:49
【问题描述】:

我需要帮助来弄清楚如何调用在地图中保存的类的成员函数。

基本上我有一个包含一个对象的映射,我试图调用它的一个成员函数,因为我不断收到我无法处理的编译器错误。 这是我目前拥有的函数调用的代码示例。

map<int, DailyReport> statContainer;    
for (auto x : statContainer)
    {
        if (x.first < yearAfter && x.first > year)
        {

            daycounter += 1;
            fullYearHtemp += x.second.getHighTemp;
            fullYearLtemp += x.second.getLowTemp;
            fullYearPercip += x.second.getPercip;
        }
    }

这甚至可能吗?我是不是搞错了?

编辑:getHighTemp、getLowTemp 和 getPercip 都是 DailyReport 类的成员函数。我需要在 DailyReport 对象位于地图内时访问这些函数。

【问题讨论】:

  • getHeightTempgetLowPercent等是成员还是成员函数?
  • 抱歉没有说明,它们是成员函数。

标签: c++ class dictionary member-functions


【解决方案1】:

应该是x.second.getHighTemp();(注意括号)吗?因为getHighTemp()是一个成员函数。

【讨论】:

  • 哇,真不敢相信我花了最后一个半小时在网上搜索并盯着它,结果最终放弃了,来到这里问我第一个问题,因为它是面对的东西-palm 值得一看。呃。
【解决方案2】:

看起来您想将它们称为成员函数,因此您需要将() 附加到它们的名称中,例如:

map<int, DailyReport> statContainer;
for (auto x : statContainer)
    {
        if (x.first < yearAfter && x.first > year)
        {

            daycounter += 1;
            fullYearHtemp += x.second.getHighTemp();
            fullYearLtemp += x.second.getLowTemp();
            fullYearPercip += x.second.getPercip();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-12
    • 2021-04-18
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多