-1

Hi all I'm having compilation error when I'm using vector iterator in a function itself.

i've tried executing the example from http://ideone.com/tTDYU5 and it works perfectly fine. However when I try to put it in a function things gets ugly why is this so?

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


}
koi_s3ng
  • 33
  • 6
  • @YuHao sorry, didn't know that cos I was advised to post this as a new question as it's on a different method. – koi_s3ng Oct 27 '13 at 08:34
  • What is getting ugly now ? You haven't posted the exact problem as per previous post ? What is `pointtwoD.xcord` and `pointtwoD.ycord` ? It looks like it should print same cords, along with different `*it` – P0W Oct 27 '13 at 08:38
  • Copy/paste the exact compiler error you're getting. – Adam Oct 27 '13 at 08:39
  • @P0W the value are all in the class PointTwoD so i'm trying print the value of X and Y cord base on the top 5 CIV value. – koi_s3ng Oct 27 '13 at 08:41

1 Answers1

0

Since "the value are all in the class PointTwoD "

Use it to print those :

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

If still this throws compilation error, you need to provide more info.

P0W
  • 46,614
  • 9
  • 72
  • 119
  • sorry, I just pick up C++ 1 week back.. it works fine now. But I have another question, if I decide to make xcord, ycord and civindex as Private. Than it would definitely affect the function here right? – koi_s3ng Oct 27 '13 at 08:52
  • @koi_s3ng It depends, in this case, I dont think it would affect. – P0W Oct 27 '13 at 08:56
  • i wish I could accept the ans but I do not have enuf points to do it. – koi_s3ng Oct 27 '13 at 09:00
  • is there any recommended sites which I can get reference from in regards to C++? – koi_s3ng Oct 27 '13 at 09:05
  • @koi_s3ng Sure : This [site](http://en.cppreference.com/w/) and [Good Books](http://stackoverflow.com/q/388242/1870232) – P0W Oct 27 '13 at 09:07