How Do I access a map private member from public function member

Hi!
basically I can I access the private map from the constructor but not from other members :-(


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  class FAnagramGenerator {
  public:
    FAnagramGenerator(){
        std::string len2[3] = {"blue", "red", "green"};
        std::string len3[3] = {"pink", "orange", "white"};
        std::string len4[3] = {"black", "yellow", "brown"};
        this->list[2] = len2;
        this->list[3] = len3;
        this->list[4] = len4;
        std::cout << this->list[3][1] << std::endl; // Works!!!
    };
    std::string getAnagram(int size) const{
        std::cout << this->list[size][1] << std::endl; // Doesnt!!
        return this->list[size][1];
    };

  private:
    std::map<int, std::string*> list;
};


thanks in advance! :-)
Last edited on
You map stores pointers to strings. You store pointers to strings that exist only in your constructor. When you try to access them outside of your constructor, the stored pointers are invalid since they point to memory you no longer have any business accessing.
Topic archived. No new replies allowed.