通过无序集访问类数据不能正常工作?

时间:2013-11-26 03:01:25

标签: c++ data-structures

尝试访问我的数据成员时遇到问题,我可以使用第二组眼睛来查看最新情况。我的所有类函数都遵循以下格式: dataType getItem()const {return item};

这是我的代码:

void listHash(int N, int MIN, string OFF, string DEF, int DOWN, int TOGO, int YARDLINE, vector<NFLData>& vdata){
    vector<NFLData> relevance;
    unordered_set<NFLData> data;

    for(int i = 0; i < vdata.size(); i++){
        data.insert(NFLData(vdata[i].getLine()));
    }
    unordered_set<NFLData>::iterator itr = data.begin();
    for(itr; itr != data.end(); ++itr){
        if(OFF == itr->getOffenseTeam()){
            if(DOWN == itr.getDown()){
            float rel;
            rel = -( (float)( abs(MIN - data->getMinute()) * (float)(5/3)) + (float)abs(TOGO - data->getYardsToGo() ) + (float)abs(YARDLINE - data->getYardline()) );
            if(DEF== itr->getOffenseTeam()){
                rel +=100;
            }
            string datas;
            datas = itr->getLine();
            relevance.push_back(NFLData(datas));
            }
        }
    }

当我尝试从迭代器调用函数时出现以下错误:

driver_broke.cpp: In function âvoid listHash(int, int, std::string, std::string, int, int, int, std::vector<NFLData, std::allocator<NFLData> >&)â:
driver_broke.cpp:167: error: âstruct std::tr1::__detail::_Hashtable_iterator<NFLData, true, false>â has no member named âgetDownâ
driver_broke.cpp:169: error: base operand of â->â has non-pointer type âstd::tr1::unordered_set<NFLData, std::tr1::hash<NFLData>, std::equal_to<NFLData>, std::allocator<NFLData> >â
driver_broke.cpp:169: error: base operand of â->â has non-pointer type âstd::tr1::unordered_set<NFLData, std::tr1::hash<NFLData>, std::equal_to<NFLData>, std::allocator<NFLData> >â
driver_broke.cpp:169: error: base operand of â->â has non-pointer type âstd::tr1::unordered_set<NFLData, std::tr1::hash<NFLData>, std::equal_to<NFLData>, std::allocator<NFLData> >â

1 个答案:

答案 0 :(得分:1)

迭代器具有指针语义。因此,您应该使用->通过迭代器访问成员:

if(DOWN == itr->getDown())
//            ^^
//           Here

在计算rel时,您引用data而不是itr

rel = -( (float)( abs(MIN - itr->getMinute()) * (float)(5/3)) + (float)abs(TOGO - itr->getYardsToGo() ) + (float)abs(YARDLINE - itr->getYardline()) );