I am trying to create a function in a class that returns a const pointer to a map. Then in a different class, I can have a function that can accept the constant pointer, declare the iterator, and copy the contents of the map into a vector. This map class to vector class is a requirement of the exercise. I have never done ptrs to maps before and I don’t have a syntax that the compiler likes. Here is my function declaration in Map:
class WordCount
{
public:
WordCount();
~WordCount();
void word_insert(std::string clean_word);
void print_all();
const std::map<std::string, int> * get_map();
private:
std::map<std::string, int> m_word_counts;
std::map<std::string, int>::iterator m_it;
std::pair<std::map<std::string, int>::iterator, bool> m_ret;
};
But when I try to define the function as such (or many variations I have tried) I get a conversion error. What below needs to change?
const map<string, int > * WordCount::get_map()
{
const map<string, int > *ptr = m_word_counts;
return ptr;
}
—
1 Answer