0

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;

}
0

1 Answer 1

2

You have declared the frequencies function like:

static map <string, int> frequencies ()

That is, it returns the map by value.

Which means

is=TourPackage::frequencies().begin();

and

is!=TourPackage::frequencies().end()

where each call returns a different object. The iterators from the different maps can not be compared to each other, attempting to do so will lead to undefined behavior.

Change the frequencies function to return a reference:

static map <string, int>& frequencies ()
//                      ^
//  Returning a reference
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe the return value also needs to be const? Depending on whether it is possible to change the map as external user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.