PLEASE HELP sort object vector

Hi, I'm newbie with C++ and I found an example which I need http://ideone.com/Eikbr4

But I've a question, how do I seperate this piece of code into .h and .cpp..

I tried to do something similar on my own but I'm having struggling with it after multiple try.. I've read many sort article on the but it's just not help..

missionplan.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class MissionPlan
{
    private:
        int sizeofarray;
        int sizeofarray2;
        int xcordi;
        int ycordi;
        static float civnum;


    public:
        MissionPlan();
        MissionPlan(int, int, float);

        int getx();
        int gety();
        float getciv();

        void stats();
        void storedata(int, int, float);
        void test();
        void displayall();
        void compute();
        void topfives();

};

missionplan.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "LocationData.h"
#include "PointTwoD.h"
#include "MissionPlan.h"


#include <iostream>
#include <string>
#include <algorithm> 
#include <vector>
#include <iterator>

using namespace std;

    vector <PointTwoD> point1;//set PointTwoD object as a vector array, name of array "point1"
    vector <PointTwoD> topfive;
    LocationData locationdata;
    PointTwoD pointtwoD;
    MissionPlan missionplan;

MissionPlan::MissionPlan()
{
    xcordi = 0;
    ycordi = 0;
    civnum = 0;
}

MissionPlan::MissionPlan(int x, int y, float civ)
{
    xcordi = x;
    ycordi = y;
    civnum = civ;
}   

int MissionPlan::getx()
{
    return pointtwoD.getxcord();
}

int MissionPlan::gety()
{
    return pointtwoD.getycord();
}

float MissionPlan::getciv()
{
    return locationdata.getCivIndex();
}

void MissionPlan::stats()
{
    string sunType;
    int earth;
    int moon;
    float particle;
    float plasma;
    int xcord;
    int ycord;

    cout<< "X axis: ";
    cin >> xcord;
    pointtwoD.setxcord(xcord);

    cout<< "y axis: ";
    cin >> ycord;
    pointtwoD.setycord(ycord);

    cout << "Suntype: ";
    cout.flush();//flush getline problem
    cin.ignore();
    getline(cin, sunType);
    locationdata.setsunType(sunType);

    cout << "No of Earth Like Planets: ";
    cin >> earth;
    locationdata.setnoOfEarthLikePlanets(earth);

    cout << "No of Earth Like Moons: ";
    cin >> moon;
    locationdata.setnoOfEarthLikeMoons(moon);

    cout << "Ave Particle Density: ";
    cin >> particle;
    locationdata.setaveParticulateDensity(particle);

    cout << "Ave Plasma Density: ";
    cin >> plasma;
    locationdata.setavePlasmaDensity(plasma);


    locationdata.computeCivIndex(sunType, earth, moon, particle, plasma);
    missionplan.test();
    missionplan.displayall();
}

void MissionPlan::test()
{

        int xcord = pointtwoD.getxcord();
        int ycord = pointtwoD.getycord();
        float civIndex = locationdata.getCivIndex();
        pointtwoD.setPointDetail(xcord, ycord, civIndex);
        point1.push_back(pointtwoD);//push/store new object into array

}



void MissionPlan::topfives()
{   
    bool sortByCiv(const PointTwoD &t1, const  PointTwoD  &t2);

    topfive.assign( point1.begin(), point1.end() ); 

    sort(topfive.begin(), topfive.end(), sortByCiv);

    for(int i=0; i < 5; i++)
    {
        topfive = point1.at(i);//display all data starting from the first index
        pointtwoD.displayPointdata();
    }

    bool sortByCiv(const  PointTwoD &t1, const  PointTwoD &t2)
    {
        return t1.getciv < t2.getciv;
    }
}
You're going along the right lines. But why do you have all those global variables? Why do they need to be global?

Also, you can't define a function inside a method like you're trying to do with that sortByCiv() function.
Topic archived. No new replies allowed.