Unordered_map gets empty after inserting an element, what am I doing wrong?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have an unordered_map object, which I fill somewhere, and when look for what I put, the map is empty.



I have a ClassA-object which has a ClassB private member. ClassB has a private member of ClassC* and an unordered_map<std::string, ClassC*> private member. It also has public functions insertClassC(std::string name, ClassC* o) and setActiveClassC(std::string name). My main()-function, has a ClassC object, calls insertClassC(), and then calls setActiveClassC().



int main()
{
ClassC mainClassC;
try
{
ClassA app;
app.getClassB().insertClassC("main", &mainClassC);
app.getClassB().setActiveClassC("main");
} catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

ClassB ClassA::getClassB()
{
return classBObject; // private member of ClassA
}

void ClassB::insertClassC(std::string name, ClassC* o)
{
// classCMap is an unordered_map<std::string, ClassC*>, private member of ClassB
classCMap.insert(std::pair<std::string, ClassC*> (name, o));
}

void ClassB::setActiveClassC(const std::string name)
{
std::unordered_map<std::string, ClassC*>::const_iterator res = classCMap.find(name);
if(res == classCMap.end())
{
char err [50];
sprintf(err, "ClassC "%s" could not be found", name.c_str());
throw std::runtime_error(err);
}
active = res->second;
}


I would expect this program to return EXIT_SUCCESS, but it returns EXIT_FAILURE and prints to stderr:



ClassC "main" could not be found


When debugging, I can see that the size of classCMap is 1 at the end of the insertClassC() call, but it's 0 when I call setActiveClassC().



I suspect my understanding of pointers is wrong (I'm kind of a beginner with c++), so I read stuff about that, but I still don't understand my mistake.










share|improve this question




















  • 1





    app.getClassB() returns a copy of your private class member. Try changing the signature of getClassB to ClassB& getClassB() and then it should do something.

    – Thomas Lang
    Dec 22 '18 at 9:16


















1















I have an unordered_map object, which I fill somewhere, and when look for what I put, the map is empty.



I have a ClassA-object which has a ClassB private member. ClassB has a private member of ClassC* and an unordered_map<std::string, ClassC*> private member. It also has public functions insertClassC(std::string name, ClassC* o) and setActiveClassC(std::string name). My main()-function, has a ClassC object, calls insertClassC(), and then calls setActiveClassC().



int main()
{
ClassC mainClassC;
try
{
ClassA app;
app.getClassB().insertClassC("main", &mainClassC);
app.getClassB().setActiveClassC("main");
} catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

ClassB ClassA::getClassB()
{
return classBObject; // private member of ClassA
}

void ClassB::insertClassC(std::string name, ClassC* o)
{
// classCMap is an unordered_map<std::string, ClassC*>, private member of ClassB
classCMap.insert(std::pair<std::string, ClassC*> (name, o));
}

void ClassB::setActiveClassC(const std::string name)
{
std::unordered_map<std::string, ClassC*>::const_iterator res = classCMap.find(name);
if(res == classCMap.end())
{
char err [50];
sprintf(err, "ClassC "%s" could not be found", name.c_str());
throw std::runtime_error(err);
}
active = res->second;
}


I would expect this program to return EXIT_SUCCESS, but it returns EXIT_FAILURE and prints to stderr:



ClassC "main" could not be found


When debugging, I can see that the size of classCMap is 1 at the end of the insertClassC() call, but it's 0 when I call setActiveClassC().



I suspect my understanding of pointers is wrong (I'm kind of a beginner with c++), so I read stuff about that, but I still don't understand my mistake.










share|improve this question




















  • 1





    app.getClassB() returns a copy of your private class member. Try changing the signature of getClassB to ClassB& getClassB() and then it should do something.

    – Thomas Lang
    Dec 22 '18 at 9:16














1












1








1








I have an unordered_map object, which I fill somewhere, and when look for what I put, the map is empty.



I have a ClassA-object which has a ClassB private member. ClassB has a private member of ClassC* and an unordered_map<std::string, ClassC*> private member. It also has public functions insertClassC(std::string name, ClassC* o) and setActiveClassC(std::string name). My main()-function, has a ClassC object, calls insertClassC(), and then calls setActiveClassC().



int main()
{
ClassC mainClassC;
try
{
ClassA app;
app.getClassB().insertClassC("main", &mainClassC);
app.getClassB().setActiveClassC("main");
} catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

ClassB ClassA::getClassB()
{
return classBObject; // private member of ClassA
}

void ClassB::insertClassC(std::string name, ClassC* o)
{
// classCMap is an unordered_map<std::string, ClassC*>, private member of ClassB
classCMap.insert(std::pair<std::string, ClassC*> (name, o));
}

void ClassB::setActiveClassC(const std::string name)
{
std::unordered_map<std::string, ClassC*>::const_iterator res = classCMap.find(name);
if(res == classCMap.end())
{
char err [50];
sprintf(err, "ClassC "%s" could not be found", name.c_str());
throw std::runtime_error(err);
}
active = res->second;
}


I would expect this program to return EXIT_SUCCESS, but it returns EXIT_FAILURE and prints to stderr:



ClassC "main" could not be found


When debugging, I can see that the size of classCMap is 1 at the end of the insertClassC() call, but it's 0 when I call setActiveClassC().



I suspect my understanding of pointers is wrong (I'm kind of a beginner with c++), so I read stuff about that, but I still don't understand my mistake.










share|improve this question
















I have an unordered_map object, which I fill somewhere, and when look for what I put, the map is empty.



I have a ClassA-object which has a ClassB private member. ClassB has a private member of ClassC* and an unordered_map<std::string, ClassC*> private member. It also has public functions insertClassC(std::string name, ClassC* o) and setActiveClassC(std::string name). My main()-function, has a ClassC object, calls insertClassC(), and then calls setActiveClassC().



int main()
{
ClassC mainClassC;
try
{
ClassA app;
app.getClassB().insertClassC("main", &mainClassC);
app.getClassB().setActiveClassC("main");
} catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

ClassB ClassA::getClassB()
{
return classBObject; // private member of ClassA
}

void ClassB::insertClassC(std::string name, ClassC* o)
{
// classCMap is an unordered_map<std::string, ClassC*>, private member of ClassB
classCMap.insert(std::pair<std::string, ClassC*> (name, o));
}

void ClassB::setActiveClassC(const std::string name)
{
std::unordered_map<std::string, ClassC*>::const_iterator res = classCMap.find(name);
if(res == classCMap.end())
{
char err [50];
sprintf(err, "ClassC "%s" could not be found", name.c_str());
throw std::runtime_error(err);
}
active = res->second;
}


I would expect this program to return EXIT_SUCCESS, but it returns EXIT_FAILURE and prints to stderr:



ClassC "main" could not be found


When debugging, I can see that the size of classCMap is 1 at the end of the insertClassC() call, but it's 0 when I call setActiveClassC().



I suspect my understanding of pointers is wrong (I'm kind of a beginner with c++), so I read stuff about that, but I still don't understand my mistake.







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 6:20







aTom

















asked Dec 22 '18 at 9:09









aTomaTom

398




398








  • 1





    app.getClassB() returns a copy of your private class member. Try changing the signature of getClassB to ClassB& getClassB() and then it should do something.

    – Thomas Lang
    Dec 22 '18 at 9:16














  • 1





    app.getClassB() returns a copy of your private class member. Try changing the signature of getClassB to ClassB& getClassB() and then it should do something.

    – Thomas Lang
    Dec 22 '18 at 9:16








1




1





app.getClassB() returns a copy of your private class member. Try changing the signature of getClassB to ClassB& getClassB() and then it should do something.

– Thomas Lang
Dec 22 '18 at 9:16





app.getClassB() returns a copy of your private class member. Try changing the signature of getClassB to ClassB& getClassB() and then it should do something.

– Thomas Lang
Dec 22 '18 at 9:16












1 Answer
1






active

oldest

votes


















2














Okay, so I decided to make my comment an answer:



Your function



ClassB ClassA::getClassB()
{
return classBObject; // private member of ClassA
}


has return type ClassB which means it copies your private member.
But since you want to modify your member, you need to return a reference to your member, i.e. you'd have to write



ClassB& ClassA::getClassB()
{
return classBObject; // private member of ClassA
}


Note the ampersand on the return type.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53894370%2funordered-map-gets-empty-after-inserting-an-element-what-am-i-doing-wrong%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Okay, so I decided to make my comment an answer:



    Your function



    ClassB ClassA::getClassB()
    {
    return classBObject; // private member of ClassA
    }


    has return type ClassB which means it copies your private member.
    But since you want to modify your member, you need to return a reference to your member, i.e. you'd have to write



    ClassB& ClassA::getClassB()
    {
    return classBObject; // private member of ClassA
    }


    Note the ampersand on the return type.






    share|improve this answer




























      2














      Okay, so I decided to make my comment an answer:



      Your function



      ClassB ClassA::getClassB()
      {
      return classBObject; // private member of ClassA
      }


      has return type ClassB which means it copies your private member.
      But since you want to modify your member, you need to return a reference to your member, i.e. you'd have to write



      ClassB& ClassA::getClassB()
      {
      return classBObject; // private member of ClassA
      }


      Note the ampersand on the return type.






      share|improve this answer


























        2












        2








        2







        Okay, so I decided to make my comment an answer:



        Your function



        ClassB ClassA::getClassB()
        {
        return classBObject; // private member of ClassA
        }


        has return type ClassB which means it copies your private member.
        But since you want to modify your member, you need to return a reference to your member, i.e. you'd have to write



        ClassB& ClassA::getClassB()
        {
        return classBObject; // private member of ClassA
        }


        Note the ampersand on the return type.






        share|improve this answer













        Okay, so I decided to make my comment an answer:



        Your function



        ClassB ClassA::getClassB()
        {
        return classBObject; // private member of ClassA
        }


        has return type ClassB which means it copies your private member.
        But since you want to modify your member, you need to return a reference to your member, i.e. you'd have to write



        ClassB& ClassA::getClassB()
        {
        return classBObject; // private member of ClassA
        }


        Note the ampersand on the return type.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 22 '18 at 9:18









        Thomas LangThomas Lang

        555311




        555311
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53894370%2funordered-map-gets-empty-after-inserting-an-element-what-am-i-doing-wrong%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas