2013-10-27 41 views
-1

大家好我有编译错误,当我在函数本身使用向量迭代器。通过向量迭代器使用函数的前5个值

我试过执行http://ideone.com/tTDYU5的例子,它工作得很好。然而,当我试图把它放在一个函数中时,事情变得很难看,为什么这样呢?

vector <PointTwoD> topfive; 
void MissionPlan::topfives() 
{ 
    topfive.assign(point1.begin(), point1.end()); 
    sort(topfive.begin(), topfive.end(), sortByCiv); 
} 

void MissionPlan::DisplayTopFiveResult() 
{ 
    missionplan.topfives(); 

    vector<PointTwoD>::iterator it = topfive.begin(); 
    for (int i = 0; i < 5 && it != topfive.end(); i++) { 
    cout << "X axis: " << pointtwoD.xcord << "   " << "Y axis: " << pointtwoD.ycord << "   " << "CIV Index: " << pointtwoD.civIndex << *it; 
    ++it; 
    } 


} 
+0

@ YuHao对不起,不知道因为它是用不同的方法,所以我建议将这个作为一个新问题发布。 –

+0

现在越来越丑陋了?您没有按照上一篇文章发布确切的问题?什么是“pointtwoD.xcord”和“pointtwoD.ycord”? 它看起来应该打印相同的电线,以及不同的'* it' – P0W

+0

复制/粘贴您正在收到的确切编译器错误。 – Adam

回答

0

由于“的值都在类PointTwoD

使用it打印那些:

vector<PointTwoD>::iterator it = topfive.begin(); 
    for (int i = 0; i < 5 && it != topfive.end(); i++) { 
    cout << "X axis: " << it->xcord << " " 
     << "Y axis: " << it->ycord << " " 
     << "CIV Index: " << it->civIndex << std::endl; 
    ++it; 
    } 

如果仍然,这将引发编译错误,您需要提供更多信息。

+0

对不起,我只是拿起C++ 1周回来..现在它工作正常。但是,如果我决定将xcord,ycord和civindex设置为Private,我还有另外一个问题。比它肯定会影响这里的功能吧? –

+0

@ koi_s3ng这取决于,在这种情况下,我不认为这会影响。 – P0W

+0

我希望我能接受答案,但我没有enuf要点。 –