C++ how to use emplace_back for user defined structure












1















I am trying to make use of emplace_back for my user defined structure:



#include <cstdint>
#include <vector>
#include <string>

struct IDNumber
{
IDNumber(std::vector<int> d) : id(d){}
std::vector<int> id;
};
struct Def
{
Def(std::initializer_list<int> id) : mid(id){}
IDNumber mid;
};

struct Student
{
std::vector<Def> ent;
};

int main()

{
Student a;
a.ent.emplace_back({ {2000} });
}


I get compilation issues:



error: no matching function for call to 'std::vector<EntryDef>::emplace_back'









share|improve this question




















  • 5





    Your first argument to your constructor is std::initializer_list<int> yet you pass { {2}, 1 }?

    – CoryKramer
    Nov 14 '18 at 14:38











  • I tried to change the line - a.ent.emplace_back({2,1,2}, FType::FD_NONE, Fmt::FMT_NONE, RWProp::PROP_RO, FId(0,TTypes::TYPE_NONE)); - but still I get the compilation issue - am I missing something?

    – Programmer
    Nov 14 '18 at 14:41






  • 4





    emplace_back is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails. Use std::initializer_list<int>{ 2, 1 }.

    – Piotr Skotnicki
    Nov 14 '18 at 14:42













  • @CoryKramer what is wrong with list-initializing the first int?

    – Piotr Skotnicki
    Nov 14 '18 at 14:44











  • @PiotrSkotnicki yeah, they kind of messed up initializer_list

    – bolov
    Nov 14 '18 at 15:00
















1















I am trying to make use of emplace_back for my user defined structure:



#include <cstdint>
#include <vector>
#include <string>

struct IDNumber
{
IDNumber(std::vector<int> d) : id(d){}
std::vector<int> id;
};
struct Def
{
Def(std::initializer_list<int> id) : mid(id){}
IDNumber mid;
};

struct Student
{
std::vector<Def> ent;
};

int main()

{
Student a;
a.ent.emplace_back({ {2000} });
}


I get compilation issues:



error: no matching function for call to 'std::vector<EntryDef>::emplace_back'









share|improve this question




















  • 5





    Your first argument to your constructor is std::initializer_list<int> yet you pass { {2}, 1 }?

    – CoryKramer
    Nov 14 '18 at 14:38











  • I tried to change the line - a.ent.emplace_back({2,1,2}, FType::FD_NONE, Fmt::FMT_NONE, RWProp::PROP_RO, FId(0,TTypes::TYPE_NONE)); - but still I get the compilation issue - am I missing something?

    – Programmer
    Nov 14 '18 at 14:41






  • 4





    emplace_back is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails. Use std::initializer_list<int>{ 2, 1 }.

    – Piotr Skotnicki
    Nov 14 '18 at 14:42













  • @CoryKramer what is wrong with list-initializing the first int?

    – Piotr Skotnicki
    Nov 14 '18 at 14:44











  • @PiotrSkotnicki yeah, they kind of messed up initializer_list

    – bolov
    Nov 14 '18 at 15:00














1












1








1








I am trying to make use of emplace_back for my user defined structure:



#include <cstdint>
#include <vector>
#include <string>

struct IDNumber
{
IDNumber(std::vector<int> d) : id(d){}
std::vector<int> id;
};
struct Def
{
Def(std::initializer_list<int> id) : mid(id){}
IDNumber mid;
};

struct Student
{
std::vector<Def> ent;
};

int main()

{
Student a;
a.ent.emplace_back({ {2000} });
}


I get compilation issues:



error: no matching function for call to 'std::vector<EntryDef>::emplace_back'









share|improve this question
















I am trying to make use of emplace_back for my user defined structure:



#include <cstdint>
#include <vector>
#include <string>

struct IDNumber
{
IDNumber(std::vector<int> d) : id(d){}
std::vector<int> id;
};
struct Def
{
Def(std::initializer_list<int> id) : mid(id){}
IDNumber mid;
};

struct Student
{
std::vector<Def> ent;
};

int main()

{
Student a;
a.ent.emplace_back({ {2000} });
}


I get compilation issues:



error: no matching function for call to 'std::vector<EntryDef>::emplace_back'






c++ c++11






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 0:48







Programmer

















asked Nov 14 '18 at 14:35









ProgrammerProgrammer

2,9201851103




2,9201851103








  • 5





    Your first argument to your constructor is std::initializer_list<int> yet you pass { {2}, 1 }?

    – CoryKramer
    Nov 14 '18 at 14:38











  • I tried to change the line - a.ent.emplace_back({2,1,2}, FType::FD_NONE, Fmt::FMT_NONE, RWProp::PROP_RO, FId(0,TTypes::TYPE_NONE)); - but still I get the compilation issue - am I missing something?

    – Programmer
    Nov 14 '18 at 14:41






  • 4





    emplace_back is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails. Use std::initializer_list<int>{ 2, 1 }.

    – Piotr Skotnicki
    Nov 14 '18 at 14:42













  • @CoryKramer what is wrong with list-initializing the first int?

    – Piotr Skotnicki
    Nov 14 '18 at 14:44











  • @PiotrSkotnicki yeah, they kind of messed up initializer_list

    – bolov
    Nov 14 '18 at 15:00














  • 5





    Your first argument to your constructor is std::initializer_list<int> yet you pass { {2}, 1 }?

    – CoryKramer
    Nov 14 '18 at 14:38











  • I tried to change the line - a.ent.emplace_back({2,1,2}, FType::FD_NONE, Fmt::FMT_NONE, RWProp::PROP_RO, FId(0,TTypes::TYPE_NONE)); - but still I get the compilation issue - am I missing something?

    – Programmer
    Nov 14 '18 at 14:41






  • 4





    emplace_back is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails. Use std::initializer_list<int>{ 2, 1 }.

    – Piotr Skotnicki
    Nov 14 '18 at 14:42













  • @CoryKramer what is wrong with list-initializing the first int?

    – Piotr Skotnicki
    Nov 14 '18 at 14:44











  • @PiotrSkotnicki yeah, they kind of messed up initializer_list

    – bolov
    Nov 14 '18 at 15:00








5




5





Your first argument to your constructor is std::initializer_list<int> yet you pass { {2}, 1 }?

– CoryKramer
Nov 14 '18 at 14:38





Your first argument to your constructor is std::initializer_list<int> yet you pass { {2}, 1 }?

– CoryKramer
Nov 14 '18 at 14:38













I tried to change the line - a.ent.emplace_back({2,1,2}, FType::FD_NONE, Fmt::FMT_NONE, RWProp::PROP_RO, FId(0,TTypes::TYPE_NONE)); - but still I get the compilation issue - am I missing something?

– Programmer
Nov 14 '18 at 14:41





I tried to change the line - a.ent.emplace_back({2,1,2}, FType::FD_NONE, Fmt::FMT_NONE, RWProp::PROP_RO, FId(0,TTypes::TYPE_NONE)); - but still I get the compilation issue - am I missing something?

– Programmer
Nov 14 '18 at 14:41




4




4





emplace_back is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails. Use std::initializer_list<int>{ 2, 1 }.

– Piotr Skotnicki
Nov 14 '18 at 14:42







emplace_back is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails. Use std::initializer_list<int>{ 2, 1 }.

– Piotr Skotnicki
Nov 14 '18 at 14:42















@CoryKramer what is wrong with list-initializing the first int?

– Piotr Skotnicki
Nov 14 '18 at 14:44





@CoryKramer what is wrong with list-initializing the first int?

– Piotr Skotnicki
Nov 14 '18 at 14:44













@PiotrSkotnicki yeah, they kind of messed up initializer_list

– bolov
Nov 14 '18 at 15:00





@PiotrSkotnicki yeah, they kind of messed up initializer_list

– bolov
Nov 14 '18 at 15:00












1 Answer
1






active

oldest

votes


















1














The comment by @PiotrSkotnicki:




emplace_back is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails.




Clarifies the problem.



An alternative way to "fix" this issue is to pass an rvalue of the needed type as argument of the constructor, instead of the initializer list:



EntryDef(ID &&id, FType ft, … ) : mid(std::forward<ID>(id)), ftype(ft), … {}


Called as:



Def a;
a.ent.emplace_back(ID{ 2, 1 }, FType::FD_NONE, …);


Live example HERE.






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%2f53302655%2fc-how-to-use-emplace-back-for-user-defined-structure%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









    1














    The comment by @PiotrSkotnicki:




    emplace_back is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails.




    Clarifies the problem.



    An alternative way to "fix" this issue is to pass an rvalue of the needed type as argument of the constructor, instead of the initializer list:



    EntryDef(ID &&id, FType ft, … ) : mid(std::forward<ID>(id)), ftype(ft), … {}


    Called as:



    Def a;
    a.ent.emplace_back(ID{ 2, 1 }, FType::FD_NONE, …);


    Live example HERE.






    share|improve this answer




























      1














      The comment by @PiotrSkotnicki:




      emplace_back is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails.




      Clarifies the problem.



      An alternative way to "fix" this issue is to pass an rvalue of the needed type as argument of the constructor, instead of the initializer list:



      EntryDef(ID &&id, FType ft, … ) : mid(std::forward<ID>(id)), ftype(ft), … {}


      Called as:



      Def a;
      a.ent.emplace_back(ID{ 2, 1 }, FType::FD_NONE, …);


      Live example HERE.






      share|improve this answer


























        1












        1








        1







        The comment by @PiotrSkotnicki:




        emplace_back is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails.




        Clarifies the problem.



        An alternative way to "fix" this issue is to pass an rvalue of the needed type as argument of the constructor, instead of the initializer list:



        EntryDef(ID &&id, FType ft, … ) : mid(std::forward<ID>(id)), ftype(ft), … {}


        Called as:



        Def a;
        a.ent.emplace_back(ID{ 2, 1 }, FType::FD_NONE, …);


        Live example HERE.






        share|improve this answer













        The comment by @PiotrSkotnicki:




        emplace_back is function template which tries to deduce the types of arguments. an initializer list does not have a type, so deduction fails.




        Clarifies the problem.



        An alternative way to "fix" this issue is to pass an rvalue of the needed type as argument of the constructor, instead of the initializer list:



        EntryDef(ID &&id, FType ft, … ) : mid(std::forward<ID>(id)), ftype(ft), … {}


        Called as:



        Def a;
        a.ent.emplace_back(ID{ 2, 1 }, FType::FD_NONE, …);


        Live example HERE.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 16:13









        Bob__Bob__

        4,89331425




        4,89331425






























            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%2f53302655%2fc-how-to-use-emplace-back-for-user-defined-structure%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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()