映射、类、成员函数

map, class, member function

本文关键字:函数 成员 映射      更新时间:2023-10-16

我需要帮助弄清楚如何调用映射中的类的成员函数。

基本上,我有一个包含对象的映射,我试图通过不断出现无法处理的编译器错误来调用它的一个成员函数。下面是我目前拥有的函数调用的代码示例。

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;
        }
    }

这可能吗?我做错了吗?

EDIT:getHighTemp、getLowTemp和getPercip都是DailyReport类的成员函数。当DailyReport对象在映射中时,我需要访问这些函数。

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

看起来您想将这些函数作为成员函数调用,因此需要将()附加到它们的名称中,类似于:

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();
        }
    }