-2

basically I can I access the private map from the constructor but not

from other members :-(

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! :-)

juanp_1982
  • 917
  • 2
  • 16
  • 37
  • 1
    Possible duplicate of [Can a local variable's memory be accessed outside its scope?](https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – 1201ProgramAlarm Jan 10 '18 at 02:26
  • 2
    Your `len2` etc variables are local variables, and go out of scope at the end of the constructor. The pointers stored in the map are then invalid outside of the constructor. – 1201ProgramAlarm Jan 10 '18 at 02:27
  • 2
    You seem to have some basic misunderstandings about C++. You should take a step back and systematically learn the language from a good book. – Baum mit Augen Jan 10 '18 at 02:29
  • 2
    'Doesn't!!' is not a problem description. You need to provide the error message you are getting. – user207421 Jan 10 '18 at 03:39

3 Answers3

2

Your three arrays:

 std::string len2[3] = {"blue", "red", "green"};
 std::string len3[3] = {"pink", "orange", "white"};
 std::string len4[3] = {"black", "yellow", "brown"};

go out of scope after construction, and you end up with dangling pointers in you map.

You should use values of type std::array instead, which offer deep copy.

Other problem: your member function getAnagram is const while operator[] of std::map isn't. You have to use at() (or find()) instead:

class FAnagramGenerator {
  public:
    FAnagramGenerator()
      : list { // better here in initialization list
         {2, { "blue",    "red", "green"}},
         {3, { "pink", "orange", "white"}},
         {4, {"black", "yellow", "brown"}}
      }
    {
        std::cout << this->list[3][1] << std::endl;
    }; // <- useless semicolon
    std::string getAnagram(int size) const{
        std::cout << this->list.at(size)[1] << std::endl;
        return this->list.at(size)[1];
    }; // <- useless semicolon

  private:
    std::map<int, std::array<std::string,3>> list;
    //            ^^^^^^^^^^^^^^^^^^^^^^^^^
};
O'Neil
  • 3,790
  • 4
  • 16
  • 30
1

This has nothing to do with private. It has to do with const.

Use at. If the key doesn't exist, operator[] on std::map will create it, so the operator requires non-const access. Using at instead of operator[], which throws an exception if the key doesn't exist, will fix your problem.

std::cout << this->list.at(size)[1] << std::endl;
return this->list.at(size)[1];

Alternatively, you could remove the const qualifier from getAnagram, but the former solution is preferable.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • 1
    You missed a major bug. – Baum mit Augen Jan 10 '18 at 02:51
  • Are you going to tell us what major bug we both missed, or are you keeping the suspense going? – Silvio Mayolo Jan 10 '18 at 02:57
  • https://stackoverflow.com/questions/48179405/how-do-i-access-a-map-private-member-from-public-function-member/48179484?noredirect=1#comment83338959_48179405 Alternatively, carefully reviewing the code would help. – Baum mit Augen Jan 10 '18 at 02:58
  • Alright, fair point. So there is a dangling pointer being left behind there that is easily missed. Perhaps you should post an answer addressing this part of the issue. – Silvio Mayolo Jan 10 '18 at 03:00
  • Nah, I find this style of "helpdesk Q/A" rather unproductive, both for OP as well as for future readers. I have given OP my best advice in a comment. C++ just cannot be learned by trial and error, it's just too weird at places. – Baum mit Augen Jan 10 '18 at 03:03
0

std::string getAnagram(int size) const{

The const in this line of code means that you cannot call any functions that are non-const from within the function.

operator[] (which is a function hidden in the notation) is non-const (see C++ const map element access)

Instead, you should use std::map::at to get the element.

Roy
  • 3,574
  • 2
  • 29
  • 39