I've created two classes: package and tourpackage that inherits the package class. The tour package class contains a vector of destinations. I've created a package vector and now I want for each destination in the vector to display the number of appearances. I've created a static map and a static function that returns the map so I can access it from the main functions. But when I try to display the map, the program displays but then says it has stopped working. If I make the map public then it works, but I want it to be private
TourPackage.h:
#ifndef TourPackage_H
#define TourPackage_H
#include "Package.h"
#include <vector>
#include <string>
#include <iostream>
#include <map>
using namespace std;
class TourPackage: public Package
{vector <string> destinations;
int lenght;
static map <string, int> frequency;
public:
TourPackage(double, int lenght);
virtual ~TourPackage();
static map <string, int> frequencies ()
{
return frequency;
}
protected:
private:
};
#endif
TourPackage.cpp:
#include "TourPackage.h"
TourPackage::TourPackage(double price,int n):Package (price)
{
string aux;
int max1=0;
lenght=n;
string x;
for (int i=0;i<lenght;i++)
{getline (cin, x);
destinations.push_back(x);
++frequency[destinations[i]];}
}
TourPackage::~TourPackage()
{
//dtor
}
main.cpp
#include <iostream>
#include <vector>
#include <string>
#include "TourPackage.h"
#include <typeinfo>
#include <map>
#include <utility>
#include <algorithm>
using namespace std;
map <string, int> TourPackage::frequency;
bool comp (const pair <string, int> &l, const pair <string, int> &r)
{
if (l.second!=r.second)
return l.second>r.second;
return l.first>r.first;
}
int main()
{TourPackage p(2,4);
vector <Package*> Packages;
Packages.push_back (new Package (1000));
Packages.push_back (new Package (4234));
Packages.push_back (new TourPackage (32,3));
Packages.push_back (new TourPackage (234, 4));
Packages.push_back(new TourPackage (43, 5));
for (map <string, int>::const_iterator is=TourPackage::frequencies().begin(); is!=TourPackage::frequencies().end();is++)
cout<<is->first<<" "<<is->second<<" ";
cout<<endl;
cout<<endl;
return 0;
}