Vector size is not correct - how can we initialize a vector inside a structure












-3














I have below sample code:



#include <iostream>
#include <vector>

typedef struct defs
{
std::vector<int> myvec;
} dvec;

typedef struct devdef: public dvec
{
dvec dvec1;
devdef()
{
dvec1.myvec.push_back(10);
}
}myVec;

class A
{
public:
A():v1()
{
std::cout << "my vec size is " << v1.myvec.size() << std::endl;
}
private:
myVec v1;
};

int main()
{
A a1;
}


Compile and execute:



$ c++ -std=c++11 try58.cpp

$ ./a.exe
my vec size is 0


I was expecting the size of vector to be 1 - why the same is 0?










share|improve this question




















  • 8




    What's up with those typedef's everywhere? You don't need them
    – Rakete1111
    Nov 12 at 14:59








  • 5




    Such confusion, much wow
    – Zereges
    Nov 12 at 15:01






  • 4




    @Programmer That's a style used in c because struct doesn't define a type name there. In c++ you don't need to typedef them since struct create type names.
    – François Andrieux
    Nov 12 at 15:01






  • 2




    dvec1.myvec.push_back(10); pushes data to a different vector, than you are trying to reference in v1.myvec.size().
    – Algirdas Preidžius
    Nov 12 at 15:02






  • 1




    I"m upvoting @Zereges because this looks like a bad transition from C to C++. Deriving a new struct from the old, and adding in another member of the same type as that already present in the base.... plus the typedef thing. I think that OP would benefit from some "compare/contrast" tutorials for C programmers moving to C++
    – Tim Randall
    Nov 12 at 15:05
















-3














I have below sample code:



#include <iostream>
#include <vector>

typedef struct defs
{
std::vector<int> myvec;
} dvec;

typedef struct devdef: public dvec
{
dvec dvec1;
devdef()
{
dvec1.myvec.push_back(10);
}
}myVec;

class A
{
public:
A():v1()
{
std::cout << "my vec size is " << v1.myvec.size() << std::endl;
}
private:
myVec v1;
};

int main()
{
A a1;
}


Compile and execute:



$ c++ -std=c++11 try58.cpp

$ ./a.exe
my vec size is 0


I was expecting the size of vector to be 1 - why the same is 0?










share|improve this question




















  • 8




    What's up with those typedef's everywhere? You don't need them
    – Rakete1111
    Nov 12 at 14:59








  • 5




    Such confusion, much wow
    – Zereges
    Nov 12 at 15:01






  • 4




    @Programmer That's a style used in c because struct doesn't define a type name there. In c++ you don't need to typedef them since struct create type names.
    – François Andrieux
    Nov 12 at 15:01






  • 2




    dvec1.myvec.push_back(10); pushes data to a different vector, than you are trying to reference in v1.myvec.size().
    – Algirdas Preidžius
    Nov 12 at 15:02






  • 1




    I"m upvoting @Zereges because this looks like a bad transition from C to C++. Deriving a new struct from the old, and adding in another member of the same type as that already present in the base.... plus the typedef thing. I think that OP would benefit from some "compare/contrast" tutorials for C programmers moving to C++
    – Tim Randall
    Nov 12 at 15:05














-3












-3








-3







I have below sample code:



#include <iostream>
#include <vector>

typedef struct defs
{
std::vector<int> myvec;
} dvec;

typedef struct devdef: public dvec
{
dvec dvec1;
devdef()
{
dvec1.myvec.push_back(10);
}
}myVec;

class A
{
public:
A():v1()
{
std::cout << "my vec size is " << v1.myvec.size() << std::endl;
}
private:
myVec v1;
};

int main()
{
A a1;
}


Compile and execute:



$ c++ -std=c++11 try58.cpp

$ ./a.exe
my vec size is 0


I was expecting the size of vector to be 1 - why the same is 0?










share|improve this question















I have below sample code:



#include <iostream>
#include <vector>

typedef struct defs
{
std::vector<int> myvec;
} dvec;

typedef struct devdef: public dvec
{
dvec dvec1;
devdef()
{
dvec1.myvec.push_back(10);
}
}myVec;

class A
{
public:
A():v1()
{
std::cout << "my vec size is " << v1.myvec.size() << std::endl;
}
private:
myVec v1;
};

int main()
{
A a1;
}


Compile and execute:



$ c++ -std=c++11 try58.cpp

$ ./a.exe
my vec size is 0


I was expecting the size of vector to be 1 - why the same is 0?







c++ c++11






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 14:59

























asked Nov 12 at 14:58









Programmer

2,8341649102




2,8341649102








  • 8




    What's up with those typedef's everywhere? You don't need them
    – Rakete1111
    Nov 12 at 14:59








  • 5




    Such confusion, much wow
    – Zereges
    Nov 12 at 15:01






  • 4




    @Programmer That's a style used in c because struct doesn't define a type name there. In c++ you don't need to typedef them since struct create type names.
    – François Andrieux
    Nov 12 at 15:01






  • 2




    dvec1.myvec.push_back(10); pushes data to a different vector, than you are trying to reference in v1.myvec.size().
    – Algirdas Preidžius
    Nov 12 at 15:02






  • 1




    I"m upvoting @Zereges because this looks like a bad transition from C to C++. Deriving a new struct from the old, and adding in another member of the same type as that already present in the base.... plus the typedef thing. I think that OP would benefit from some "compare/contrast" tutorials for C programmers moving to C++
    – Tim Randall
    Nov 12 at 15:05














  • 8




    What's up with those typedef's everywhere? You don't need them
    – Rakete1111
    Nov 12 at 14:59








  • 5




    Such confusion, much wow
    – Zereges
    Nov 12 at 15:01






  • 4




    @Programmer That's a style used in c because struct doesn't define a type name there. In c++ you don't need to typedef them since struct create type names.
    – François Andrieux
    Nov 12 at 15:01






  • 2




    dvec1.myvec.push_back(10); pushes data to a different vector, than you are trying to reference in v1.myvec.size().
    – Algirdas Preidžius
    Nov 12 at 15:02






  • 1




    I"m upvoting @Zereges because this looks like a bad transition from C to C++. Deriving a new struct from the old, and adding in another member of the same type as that already present in the base.... plus the typedef thing. I think that OP would benefit from some "compare/contrast" tutorials for C programmers moving to C++
    – Tim Randall
    Nov 12 at 15:05








8




8




What's up with those typedef's everywhere? You don't need them
– Rakete1111
Nov 12 at 14:59






What's up with those typedef's everywhere? You don't need them
– Rakete1111
Nov 12 at 14:59






5




5




Such confusion, much wow
– Zereges
Nov 12 at 15:01




Such confusion, much wow
– Zereges
Nov 12 at 15:01




4




4




@Programmer That's a style used in c because struct doesn't define a type name there. In c++ you don't need to typedef them since struct create type names.
– François Andrieux
Nov 12 at 15:01




@Programmer That's a style used in c because struct doesn't define a type name there. In c++ you don't need to typedef them since struct create type names.
– François Andrieux
Nov 12 at 15:01




2




2




dvec1.myvec.push_back(10); pushes data to a different vector, than you are trying to reference in v1.myvec.size().
– Algirdas Preidžius
Nov 12 at 15:02




dvec1.myvec.push_back(10); pushes data to a different vector, than you are trying to reference in v1.myvec.size().
– Algirdas Preidžius
Nov 12 at 15:02




1




1




I"m upvoting @Zereges because this looks like a bad transition from C to C++. Deriving a new struct from the old, and adding in another member of the same type as that already present in the base.... plus the typedef thing. I think that OP would benefit from some "compare/contrast" tutorials for C programmers moving to C++
– Tim Randall
Nov 12 at 15:05




I"m upvoting @Zereges because this looks like a bad transition from C to C++. Deriving a new struct from the old, and adding in another member of the same type as that already present in the base.... plus the typedef thing. I think that OP would benefit from some "compare/contrast" tutorials for C programmers moving to C++
– Tim Randall
Nov 12 at 15:05












2 Answers
2






active

oldest

votes


















0














This version, I think, does what you wanted.



#include <iostream>
#include <vector>

struct dvec
{
std::vector<int> myvec;
}

struct myVec : public dvec
{
myVec()
{
myvec.push_back(10);
}
}

class A
{
public:
A():v1()
{
std::cout << "my vec size is " << v1.myvec.size() << std::endl;
}
private:
myVec v1;
};

int main()
{
A a1;
}


Some notes about what I've changed.




  1. There's no need to use a typedef to create a new type. In C++ both class and struct define new types.

  2. When deriving the new type myVec from dvec, it contains the vectror myvec, so there is no need to add a new one.

  3. In the constructor for the derived type myVec, we can access the myvec member of the base type directly, since it is a member of this object.






share|improve this answer





























    2














    You have dvecs as both a parent to myVec as well as a member of myVec. In your constructor you push a value to dvec1.myvec, but in A you read v1.myvec aka the member of the parent. You simply have two instances of dvecs in your structure and you're using different ones in your functions.






    share|improve this answer





















    • Thanks after I read v1.dvec1.myvec.size() I was able to get the correct size
      – Programmer
      Nov 12 at 15:07











    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%2f53264778%2fvector-size-is-not-correct-how-can-we-initialize-a-vector-inside-a-structure%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









    0














    This version, I think, does what you wanted.



    #include <iostream>
    #include <vector>

    struct dvec
    {
    std::vector<int> myvec;
    }

    struct myVec : public dvec
    {
    myVec()
    {
    myvec.push_back(10);
    }
    }

    class A
    {
    public:
    A():v1()
    {
    std::cout << "my vec size is " << v1.myvec.size() << std::endl;
    }
    private:
    myVec v1;
    };

    int main()
    {
    A a1;
    }


    Some notes about what I've changed.




    1. There's no need to use a typedef to create a new type. In C++ both class and struct define new types.

    2. When deriving the new type myVec from dvec, it contains the vectror myvec, so there is no need to add a new one.

    3. In the constructor for the derived type myVec, we can access the myvec member of the base type directly, since it is a member of this object.






    share|improve this answer


























      0














      This version, I think, does what you wanted.



      #include <iostream>
      #include <vector>

      struct dvec
      {
      std::vector<int> myvec;
      }

      struct myVec : public dvec
      {
      myVec()
      {
      myvec.push_back(10);
      }
      }

      class A
      {
      public:
      A():v1()
      {
      std::cout << "my vec size is " << v1.myvec.size() << std::endl;
      }
      private:
      myVec v1;
      };

      int main()
      {
      A a1;
      }


      Some notes about what I've changed.




      1. There's no need to use a typedef to create a new type. In C++ both class and struct define new types.

      2. When deriving the new type myVec from dvec, it contains the vectror myvec, so there is no need to add a new one.

      3. In the constructor for the derived type myVec, we can access the myvec member of the base type directly, since it is a member of this object.






      share|improve this answer
























        0












        0








        0






        This version, I think, does what you wanted.



        #include <iostream>
        #include <vector>

        struct dvec
        {
        std::vector<int> myvec;
        }

        struct myVec : public dvec
        {
        myVec()
        {
        myvec.push_back(10);
        }
        }

        class A
        {
        public:
        A():v1()
        {
        std::cout << "my vec size is " << v1.myvec.size() << std::endl;
        }
        private:
        myVec v1;
        };

        int main()
        {
        A a1;
        }


        Some notes about what I've changed.




        1. There's no need to use a typedef to create a new type. In C++ both class and struct define new types.

        2. When deriving the new type myVec from dvec, it contains the vectror myvec, so there is no need to add a new one.

        3. In the constructor for the derived type myVec, we can access the myvec member of the base type directly, since it is a member of this object.






        share|improve this answer












        This version, I think, does what you wanted.



        #include <iostream>
        #include <vector>

        struct dvec
        {
        std::vector<int> myvec;
        }

        struct myVec : public dvec
        {
        myVec()
        {
        myvec.push_back(10);
        }
        }

        class A
        {
        public:
        A():v1()
        {
        std::cout << "my vec size is " << v1.myvec.size() << std::endl;
        }
        private:
        myVec v1;
        };

        int main()
        {
        A a1;
        }


        Some notes about what I've changed.




        1. There's no need to use a typedef to create a new type. In C++ both class and struct define new types.

        2. When deriving the new type myVec from dvec, it contains the vectror myvec, so there is no need to add a new one.

        3. In the constructor for the derived type myVec, we can access the myvec member of the base type directly, since it is a member of this object.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 15:19









        Tim Randall

        1,9221119




        1,9221119

























            2














            You have dvecs as both a parent to myVec as well as a member of myVec. In your constructor you push a value to dvec1.myvec, but in A you read v1.myvec aka the member of the parent. You simply have two instances of dvecs in your structure and you're using different ones in your functions.






            share|improve this answer





















            • Thanks after I read v1.dvec1.myvec.size() I was able to get the correct size
              – Programmer
              Nov 12 at 15:07
















            2














            You have dvecs as both a parent to myVec as well as a member of myVec. In your constructor you push a value to dvec1.myvec, but in A you read v1.myvec aka the member of the parent. You simply have two instances of dvecs in your structure and you're using different ones in your functions.






            share|improve this answer





















            • Thanks after I read v1.dvec1.myvec.size() I was able to get the correct size
              – Programmer
              Nov 12 at 15:07














            2












            2








            2






            You have dvecs as both a parent to myVec as well as a member of myVec. In your constructor you push a value to dvec1.myvec, but in A you read v1.myvec aka the member of the parent. You simply have two instances of dvecs in your structure and you're using different ones in your functions.






            share|improve this answer












            You have dvecs as both a parent to myVec as well as a member of myVec. In your constructor you push a value to dvec1.myvec, but in A you read v1.myvec aka the member of the parent. You simply have two instances of dvecs in your structure and you're using different ones in your functions.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 at 15:03









            Kiskae

            12.3k12538




            12.3k12538












            • Thanks after I read v1.dvec1.myvec.size() I was able to get the correct size
              – Programmer
              Nov 12 at 15:07


















            • Thanks after I read v1.dvec1.myvec.size() I was able to get the correct size
              – Programmer
              Nov 12 at 15:07
















            Thanks after I read v1.dvec1.myvec.size() I was able to get the correct size
            – Programmer
            Nov 12 at 15:07




            Thanks after I read v1.dvec1.myvec.size() I was able to get the correct size
            – Programmer
            Nov 12 at 15:07


















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53264778%2fvector-size-is-not-correct-how-can-we-initialize-a-vector-inside-a-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







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine