c++ error: passing ‘const std::vector’ as ‘this’ argument discards qualifiers [-fpermissive]










0















I have this error when i try to insert an object into a set of a stl vector, Node is a class that i have created. ¿Could you explain to me how to solve this error?



Code:



void car::expandnode(Node& current_node,std::set<vector<Node> >& closed_list,grid& map){

int c_a = current_node.get_x();
int c_b= current_node.get_y();

if((map.get(c_a,++c_b))!=1)

point p(c_a,++c_b);
Node ns(p,costtan);

costtan= 1+ heuristic1(ns); // cost to arrive node

if(std::find((*closed_list.end()).begin(),(*closed_list.end()).end(),ns)==((*closed_list.end()).end()))
closed_list.end()->push_back(ns); //error




float car::heuristic1(Nodo& successor)
int c1= successor.get_x();
int c2= successor.get_y();
float result = sqrt(pow((c1-destination_point_.get_x()),2) + pow((c2-destination_point_.get_y()),2));

return resultado;




Error:



 error: passing ‘const std::vector<Node>’ as ‘this’ argument discards qualifiers [-fpermissive]
closed_list.end()->push_back(ns);


Thanks



EDIT1:



I am implementing the A star algorithm, so in the function expandnode if a successor is not an obstacle (!=1), i insert it in the set of vector, this set contains the successors nodes of each node, i added more code to the question to clarify my doubt



Because i am calling expandnode() from other function (main A-star function), i need to insert the succesors of each node on the last vector of the set, i use .end() for that purpose, because initially each vector have one node, once made the call to expandnode() this vector have the node and its successors










share|improve this question



















  • 2





    closed_list.end()->push_back(ns); -- What exactly are you trying to accomplish with that line of code? Whatever it is, that code doesn't do it.

    – PaulMcKenzie
    Nov 14 '18 at 1:46







  • 1





    Dereferencing .end() is undefined behavior as it only acts as a placeholder. It points one past the last element.

    – DeiDei
    Nov 14 '18 at 1:49











  • *closed_list.end() -- You do this in multiple places. This is completely wrong (dereferencing the end() iterator). So you should state what you're trying to do with these lines.

    – PaulMcKenzie
    Nov 14 '18 at 1:50












  • @PaulMcKenzie I edited the question

    – AER
    Nov 14 '18 at 9:16











  • @AER The last element of a set is closed_list.rbegin(). Dereferencing end() is undefined behavior.

    – PaulMcKenzie
    Nov 14 '18 at 11:09
















0















I have this error when i try to insert an object into a set of a stl vector, Node is a class that i have created. ¿Could you explain to me how to solve this error?



Code:



void car::expandnode(Node& current_node,std::set<vector<Node> >& closed_list,grid& map){

int c_a = current_node.get_x();
int c_b= current_node.get_y();

if((map.get(c_a,++c_b))!=1)

point p(c_a,++c_b);
Node ns(p,costtan);

costtan= 1+ heuristic1(ns); // cost to arrive node

if(std::find((*closed_list.end()).begin(),(*closed_list.end()).end(),ns)==((*closed_list.end()).end()))
closed_list.end()->push_back(ns); //error




float car::heuristic1(Nodo& successor)
int c1= successor.get_x();
int c2= successor.get_y();
float result = sqrt(pow((c1-destination_point_.get_x()),2) + pow((c2-destination_point_.get_y()),2));

return resultado;




Error:



 error: passing ‘const std::vector<Node>’ as ‘this’ argument discards qualifiers [-fpermissive]
closed_list.end()->push_back(ns);


Thanks



EDIT1:



I am implementing the A star algorithm, so in the function expandnode if a successor is not an obstacle (!=1), i insert it in the set of vector, this set contains the successors nodes of each node, i added more code to the question to clarify my doubt



Because i am calling expandnode() from other function (main A-star function), i need to insert the succesors of each node on the last vector of the set, i use .end() for that purpose, because initially each vector have one node, once made the call to expandnode() this vector have the node and its successors










share|improve this question



















  • 2





    closed_list.end()->push_back(ns); -- What exactly are you trying to accomplish with that line of code? Whatever it is, that code doesn't do it.

    – PaulMcKenzie
    Nov 14 '18 at 1:46







  • 1





    Dereferencing .end() is undefined behavior as it only acts as a placeholder. It points one past the last element.

    – DeiDei
    Nov 14 '18 at 1:49











  • *closed_list.end() -- You do this in multiple places. This is completely wrong (dereferencing the end() iterator). So you should state what you're trying to do with these lines.

    – PaulMcKenzie
    Nov 14 '18 at 1:50












  • @PaulMcKenzie I edited the question

    – AER
    Nov 14 '18 at 9:16











  • @AER The last element of a set is closed_list.rbegin(). Dereferencing end() is undefined behavior.

    – PaulMcKenzie
    Nov 14 '18 at 11:09














0












0








0








I have this error when i try to insert an object into a set of a stl vector, Node is a class that i have created. ¿Could you explain to me how to solve this error?



Code:



void car::expandnode(Node& current_node,std::set<vector<Node> >& closed_list,grid& map){

int c_a = current_node.get_x();
int c_b= current_node.get_y();

if((map.get(c_a,++c_b))!=1)

point p(c_a,++c_b);
Node ns(p,costtan);

costtan= 1+ heuristic1(ns); // cost to arrive node

if(std::find((*closed_list.end()).begin(),(*closed_list.end()).end(),ns)==((*closed_list.end()).end()))
closed_list.end()->push_back(ns); //error




float car::heuristic1(Nodo& successor)
int c1= successor.get_x();
int c2= successor.get_y();
float result = sqrt(pow((c1-destination_point_.get_x()),2) + pow((c2-destination_point_.get_y()),2));

return resultado;




Error:



 error: passing ‘const std::vector<Node>’ as ‘this’ argument discards qualifiers [-fpermissive]
closed_list.end()->push_back(ns);


Thanks



EDIT1:



I am implementing the A star algorithm, so in the function expandnode if a successor is not an obstacle (!=1), i insert it in the set of vector, this set contains the successors nodes of each node, i added more code to the question to clarify my doubt



Because i am calling expandnode() from other function (main A-star function), i need to insert the succesors of each node on the last vector of the set, i use .end() for that purpose, because initially each vector have one node, once made the call to expandnode() this vector have the node and its successors










share|improve this question
















I have this error when i try to insert an object into a set of a stl vector, Node is a class that i have created. ¿Could you explain to me how to solve this error?



Code:



void car::expandnode(Node& current_node,std::set<vector<Node> >& closed_list,grid& map){

int c_a = current_node.get_x();
int c_b= current_node.get_y();

if((map.get(c_a,++c_b))!=1)

point p(c_a,++c_b);
Node ns(p,costtan);

costtan= 1+ heuristic1(ns); // cost to arrive node

if(std::find((*closed_list.end()).begin(),(*closed_list.end()).end(),ns)==((*closed_list.end()).end()))
closed_list.end()->push_back(ns); //error




float car::heuristic1(Nodo& successor)
int c1= successor.get_x();
int c2= successor.get_y();
float result = sqrt(pow((c1-destination_point_.get_x()),2) + pow((c2-destination_point_.get_y()),2));

return resultado;




Error:



 error: passing ‘const std::vector<Node>’ as ‘this’ argument discards qualifiers [-fpermissive]
closed_list.end()->push_back(ns);


Thanks



EDIT1:



I am implementing the A star algorithm, so in the function expandnode if a successor is not an obstacle (!=1), i insert it in the set of vector, this set contains the successors nodes of each node, i added more code to the question to clarify my doubt



Because i am calling expandnode() from other function (main A-star function), i need to insert the succesors of each node on the last vector of the set, i use .end() for that purpose, because initially each vector have one node, once made the call to expandnode() this vector have the node and its successors







c++ object compilation iterator set






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 9:41







AER

















asked Nov 14 '18 at 1:42









AERAER

1137




1137







  • 2





    closed_list.end()->push_back(ns); -- What exactly are you trying to accomplish with that line of code? Whatever it is, that code doesn't do it.

    – PaulMcKenzie
    Nov 14 '18 at 1:46







  • 1





    Dereferencing .end() is undefined behavior as it only acts as a placeholder. It points one past the last element.

    – DeiDei
    Nov 14 '18 at 1:49











  • *closed_list.end() -- You do this in multiple places. This is completely wrong (dereferencing the end() iterator). So you should state what you're trying to do with these lines.

    – PaulMcKenzie
    Nov 14 '18 at 1:50












  • @PaulMcKenzie I edited the question

    – AER
    Nov 14 '18 at 9:16











  • @AER The last element of a set is closed_list.rbegin(). Dereferencing end() is undefined behavior.

    – PaulMcKenzie
    Nov 14 '18 at 11:09













  • 2





    closed_list.end()->push_back(ns); -- What exactly are you trying to accomplish with that line of code? Whatever it is, that code doesn't do it.

    – PaulMcKenzie
    Nov 14 '18 at 1:46







  • 1





    Dereferencing .end() is undefined behavior as it only acts as a placeholder. It points one past the last element.

    – DeiDei
    Nov 14 '18 at 1:49











  • *closed_list.end() -- You do this in multiple places. This is completely wrong (dereferencing the end() iterator). So you should state what you're trying to do with these lines.

    – PaulMcKenzie
    Nov 14 '18 at 1:50












  • @PaulMcKenzie I edited the question

    – AER
    Nov 14 '18 at 9:16











  • @AER The last element of a set is closed_list.rbegin(). Dereferencing end() is undefined behavior.

    – PaulMcKenzie
    Nov 14 '18 at 11:09








2




2





closed_list.end()->push_back(ns); -- What exactly are you trying to accomplish with that line of code? Whatever it is, that code doesn't do it.

– PaulMcKenzie
Nov 14 '18 at 1:46






closed_list.end()->push_back(ns); -- What exactly are you trying to accomplish with that line of code? Whatever it is, that code doesn't do it.

– PaulMcKenzie
Nov 14 '18 at 1:46





1




1





Dereferencing .end() is undefined behavior as it only acts as a placeholder. It points one past the last element.

– DeiDei
Nov 14 '18 at 1:49





Dereferencing .end() is undefined behavior as it only acts as a placeholder. It points one past the last element.

– DeiDei
Nov 14 '18 at 1:49













*closed_list.end() -- You do this in multiple places. This is completely wrong (dereferencing the end() iterator). So you should state what you're trying to do with these lines.

– PaulMcKenzie
Nov 14 '18 at 1:50






*closed_list.end() -- You do this in multiple places. This is completely wrong (dereferencing the end() iterator). So you should state what you're trying to do with these lines.

– PaulMcKenzie
Nov 14 '18 at 1:50














@PaulMcKenzie I edited the question

– AER
Nov 14 '18 at 9:16





@PaulMcKenzie I edited the question

– AER
Nov 14 '18 at 9:16













@AER The last element of a set is closed_list.rbegin(). Dereferencing end() is undefined behavior.

– PaulMcKenzie
Nov 14 '18 at 11:09






@AER The last element of a set is closed_list.rbegin(). Dereferencing end() is undefined behavior.

– PaulMcKenzie
Nov 14 '18 at 11:09













2 Answers
2






active

oldest

votes


















2














closed_list is a std::set. The objects stored in a set are const objects, since modifying them can potentially change how the modified object sorts in relation to the other objects in the set.



When you dereference an iterator of a set, the reference you get is a const &. When you then try calling push_back on that object, the compiler generates an error because push_back cannot be called on a const object.



Another problem with the code is that the end iterator of any standard container is not dereferencable, and doing so as you do results in Undefined Behavior. Perhaps you meant to use back() instead, which returns a reference to the last item in the container?



This still won't address the problem of modifying an object in a set. To do that, you need to erase the object from the set, modify it, then reinsert the modified object.






share|improve this answer























  • rbegin() gets the iterator to the last element of the set.

    – PaulMcKenzie
    Nov 14 '18 at 11:10


















1














Another problem is that all elements in a set are ordered by operator < by default. You may want to override it.






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%2f53291983%2fc-error-passing-const-stdvectornode-as-this-argument-discards-qualifi%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    closed_list is a std::set. The objects stored in a set are const objects, since modifying them can potentially change how the modified object sorts in relation to the other objects in the set.



    When you dereference an iterator of a set, the reference you get is a const &. When you then try calling push_back on that object, the compiler generates an error because push_back cannot be called on a const object.



    Another problem with the code is that the end iterator of any standard container is not dereferencable, and doing so as you do results in Undefined Behavior. Perhaps you meant to use back() instead, which returns a reference to the last item in the container?



    This still won't address the problem of modifying an object in a set. To do that, you need to erase the object from the set, modify it, then reinsert the modified object.






    share|improve this answer























    • rbegin() gets the iterator to the last element of the set.

      – PaulMcKenzie
      Nov 14 '18 at 11:10















    2














    closed_list is a std::set. The objects stored in a set are const objects, since modifying them can potentially change how the modified object sorts in relation to the other objects in the set.



    When you dereference an iterator of a set, the reference you get is a const &. When you then try calling push_back on that object, the compiler generates an error because push_back cannot be called on a const object.



    Another problem with the code is that the end iterator of any standard container is not dereferencable, and doing so as you do results in Undefined Behavior. Perhaps you meant to use back() instead, which returns a reference to the last item in the container?



    This still won't address the problem of modifying an object in a set. To do that, you need to erase the object from the set, modify it, then reinsert the modified object.






    share|improve this answer























    • rbegin() gets the iterator to the last element of the set.

      – PaulMcKenzie
      Nov 14 '18 at 11:10













    2












    2








    2







    closed_list is a std::set. The objects stored in a set are const objects, since modifying them can potentially change how the modified object sorts in relation to the other objects in the set.



    When you dereference an iterator of a set, the reference you get is a const &. When you then try calling push_back on that object, the compiler generates an error because push_back cannot be called on a const object.



    Another problem with the code is that the end iterator of any standard container is not dereferencable, and doing so as you do results in Undefined Behavior. Perhaps you meant to use back() instead, which returns a reference to the last item in the container?



    This still won't address the problem of modifying an object in a set. To do that, you need to erase the object from the set, modify it, then reinsert the modified object.






    share|improve this answer













    closed_list is a std::set. The objects stored in a set are const objects, since modifying them can potentially change how the modified object sorts in relation to the other objects in the set.



    When you dereference an iterator of a set, the reference you get is a const &. When you then try calling push_back on that object, the compiler generates an error because push_back cannot be called on a const object.



    Another problem with the code is that the end iterator of any standard container is not dereferencable, and doing so as you do results in Undefined Behavior. Perhaps you meant to use back() instead, which returns a reference to the last item in the container?



    This still won't address the problem of modifying an object in a set. To do that, you need to erase the object from the set, modify it, then reinsert the modified object.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 14 '18 at 2:20









    1201ProgramAlarm1201ProgramAlarm

    16.6k42539




    16.6k42539












    • rbegin() gets the iterator to the last element of the set.

      – PaulMcKenzie
      Nov 14 '18 at 11:10

















    • rbegin() gets the iterator to the last element of the set.

      – PaulMcKenzie
      Nov 14 '18 at 11:10
















    rbegin() gets the iterator to the last element of the set.

    – PaulMcKenzie
    Nov 14 '18 at 11:10





    rbegin() gets the iterator to the last element of the set.

    – PaulMcKenzie
    Nov 14 '18 at 11:10













    1














    Another problem is that all elements in a set are ordered by operator < by default. You may want to override it.






    share|improve this answer



























      1














      Another problem is that all elements in a set are ordered by operator < by default. You may want to override it.






      share|improve this answer

























        1












        1








        1







        Another problem is that all elements in a set are ordered by operator < by default. You may want to override it.






        share|improve this answer













        Another problem is that all elements in a set are ordered by operator < by default. You may want to override it.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 6:49









        EiniemandEiniemand

        495




        495



























            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%2f53291983%2fc-error-passing-const-stdvectornode-as-this-argument-discards-qualifi%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

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線