std:: vector push_back a struct

Discussion in 'C++' started by jmsanchezdiaz, Dec 13, 2007.

  1. CPP question: if i had a struct like "struct str { int a; int b };"
    and a vector "std::vector < str > test;" and wanted to push_back a
    struct, would i have to define the struct, fill it, and then push_back
    it, or could i pushback the two ints directly somehow?

    Thanks for all.
     
    jmsanchezdiaz, Dec 13, 2007
    #1
    1. Advertising

  2. On Dec 13, 6:23 pm, jmsanchezdiaz <> wrote:
    > CPP question: if i had a struct like "struct str { int a; int b };"
    > and a vector "std::vector < str > test;" and wanted to push_back a
    > struct, would i have to define the struct, fill it, and then push_back
    > it, or could i pushback the two ints directly somehow?
    >
    > Thanks for all.


    Hi.

    As far as I know, You have to define a struct, fill it and push_back
    it.
    If you defined the vector as "std::vector<str> test;", there is no way
    to push_back
    data members of struct str directly.

    And it is not difficult to do so, give it a chance.

    Y.w
     
    The Last Ottoman, Dec 13, 2007
    #2
    1. Advertising

  3. jmsanchezdiaz

    LR Guest

    jmsanchezdiaz wrote:
    > CPP question: if i had a struct like "struct str { int a; int b };"
    > and a vector "std::vector < str > test;" and wanted to push_back a
    > struct, would i have to define the struct, fill it, and then push_back
    > it, or could i pushback the two ints directly somehow?


    You have to make an instance of the struct, yes. You can't push_back
    ints onto a std::vector<str>.

    But ctors don't always have to be called explicitly when you push.

    for example, if str had a ctor like:
    str::str(const char *p) : a(0), b(0) { ... }

    then you could
    test.push_back("hello");
    test.push_back("99 44");

    Or you can call a ctor as the argument to push_back. If you have this ctor:
    str(const int aa, const int bb) : a(aa), b(bb) {}
    then
    test.push_back(str(5,4));
    test.push_back(str(99,12));

    What is it that you want to do?

    LR
     
    LR, Dec 13, 2007
    #3
  4. jmsanchezdiaz

    Daniel T. Guest

    jmsanchezdiaz <> wrote:

    > CPP question: if i had a struct like "struct str { int a; int b };"
    > and a vector "std::vector < str > test;" and wanted to push_back a
    > struct, would i have to define the struct, fill it, and then push_back
    > it, or could i pushback the two ints directly somehow?


    First choice:

    struct str { int a; int b; str(int a, int b):a(a), b(b) { } };

    int main() {
    std::vector< str > test;
    test.push_back( str( 23, 44 ) );
    }

    Second choice (if you aren't allowed to change the struct.)

    struct str { int a; int b; };

    str make_str( int a, int b ) { str s; s.a = a; s.b = b; return s; }

    int main() {
    std::vector< str > test;
    test.push_back( make_str( 23, 44 ) );
    }

    Or if you really want to show off:

    struct str { int a; int b; };

    struct my_str : str { my_str( int a_, int b_ ) { a = a_; b = b_; } };

    int main() {
    std::vector< str > test;
    test.push_back( my_str( 23, 44 ) );
    }
     
    Daniel T., Dec 13, 2007
    #4
  5. On 13 dic, 21:41, "Daniel T." <> wrote:
    > jmsanchezdiaz <> wrote:
    > > CPP question: if i had a struct like "struct str { int a; int b };"
    > > and a vector "std::vector < str > test;" and wanted to push_back a
    > > struct, would i have to define the struct, fill it, and then push_back
    > > it, or could i pushback the two ints directly somehow?

    >
    > First choice:
    >
    > struct str { int a; int b; str(int a, int b):a(a), b(b) { } };
    >
    > int main() {
    > std::vector< str > test;
    > test.push_back( str( 23, 44 ) );
    >
    > }
    >
    > Second choice (if you aren't allowed to change the struct.)
    >
    > struct str { int a; int b; };
    >
    > str make_str( int a, int b ) { str s; s.a = a; s.b = b; return s; }
    >
    > int main() {
    > std::vector< str > test;
    > test.push_back( make_str( 23, 44 ) );
    >
    > }
    >
    > Or if you really want to show off:
    >
    > struct str { int a; int b; };
    >
    > struct my_str : str { my_str( int a_, int b_ ) { a = a_; b = b_; } };
    >
    > int main() {
    > std::vector< str > test;
    > test.push_back( my_str( 23, 44 ) );
    >
    > }


    I explain my question with more detail:

    I have a:

    typedef struct
    {
    CnovaMsgTypes id; // packet type identificator
    char* data; //data of the structure

    } data_foto;

    vector< vector< data_foto > >vector_fotos;

    And I want to access the fields of the struct for doing an assignement
    in a case. I do this in a piece of my code:

    switch(pfc[j].id)
    {
    case CONTROL_HV:

    addPacketToSendBuffer( CONTROL_HV, 0, sizeof(T_ControlHV), (char *)
    &h2pcnova->h2ig.cnt );
    if (take_foto)
    {
    vector_fotos[minuto_actual][(int)(pfc[j].id)].id =
    pfc[j].id;
    vector_fotos[minuto_actual][(int)(pfc[j].id)].data
    = (char*)&(h2pcnova->h2ig.cnt);
    }

    [...]

    but when I debbug vector_fotos[minuto_actual] appears: "class
    std::vector< data_foto, std::allocator< data_foto >&)0x0 Cannot
    access"

    What's the problem??

    Thanks
     
    jmsanchezdiaz, Dec 18, 2007
    #5
  6. jmsanchezdiaz

    James Kanze Guest

    On Dec 18, 12:59 pm, jmsanchezdiaz <> wrote:
    > On 13 dic, 21:41, "Daniel T." <> wrote:
    > > jmsanchezdiaz <> wrote:

    > I explain my question with more detail:


    > I have a:


    > typedef struct
    > {
    > CnovaMsgTypes id; // packet type identificator
    > char* data; //data of the structure
    > } data_foto;


    In a header shared with C, no doubt. Otherwise, there's no need
    for the typedef, and std::string (or std::vector<char>) would
    doubtlessly be preferable to the char*. Also, you haven't shown
    us the type of id. That could be important.

    > vector< vector< data_foto > >vector_fotos;


    > And I want to access the fields of the struct for doing an
    > assignement in a case. I do this in a piece of my code:


    > switch(pfc[j].id)
    > {
    > case CONTROL_HV:


    > addPacketToSendBuffer( CONTROL_HV, 0, sizeof(T_ControlHV), (char *)
    > &h2pcnova->h2ig.cnt );
    > if (take_foto)
    > {
    > vector_fotos[minuto_actual][(int)(pfc[j].id)].id =
    > pfc[j].id;
    > vector_fotos[minuto_actual][(int)(pfc[j].id)].data
    > = (char*)&(h2pcnova->h2ig.cnt);
    > }


    > [...]


    > but when I debbug vector_fotos[minuto_actual] appears: "class
    > std::vector< data_foto, std::allocator< data_foto >&)0x0 Cannot
    > access"


    > What's the problem??


    Who knows? There's no where near enough information. If it's
    really a case of your dereferencing a null pointer (which is
    what the error message suggests), either you have a null pointer
    somewhere yourself, or one of the vectors you access is in fact
    empty.

    Try compiling with debug turned on for the STL and use whatever
    memory checkers you have. (With g++ under Linux, this would
    mean "-D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG
    -D_GLIBCXX_DEBUG_PEDANTIC" and valgrind, for example.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Dec 19, 2007
    #6
  7. On Dec 18, 11:59 am, jmsanchezdiaz <> wrote:
    > On 13 dic, 21:41, "Daniel T." <> wrote:
    >
    >
    >
    >
    >
    > > jmsanchezdiaz <> wrote:
    > > > CPP question: if i had a struct like "struct str { int a; int b };"
    > > > and a vector "std::vector < str > test;" and wanted to push_back a
    > > > struct, would i have to define the struct, fill it, and then push_back
    > > > it, or could i pushback the two ints directly somehow?

    >
    > > First choice:

    >
    > > struct str { int a; int b; str(int a, int b):a(a), b(b) { } };

    >
    > > int main() {
    > > std::vector< str > test;
    > > test.push_back( str( 23, 44 ) );

    >
    > > }

    >
    > > Second choice (if you aren't allowed to change the struct.)

    >
    > > struct str { int a; int b; };

    >
    > > str make_str( int a, int b ) { str s; s.a = a; s.b = b; return s; }

    >
    > > int main() {
    > > std::vector< str > test;
    > > test.push_back( make_str( 23, 44 ) );

    >
    > > }

    >
    > > Or if you really want to show off:

    >
    > > struct str { int a; int b; };

    >
    > > struct my_str : str { my_str( int a_, int b_ ) { a = a_; b = b_; } };

    >
    > > int main() {
    > > std::vector< str > test;
    > > test.push_back( my_str( 23, 44 ) );

    >
    > > }

    >
    > I explain my question with more detail:
    >
    > I have a:
    >
    > typedef struct
    > {
    > CnovaMsgTypes id; // packet type identificator
    > char* data; //data of the structure
    >
    > } data_foto;
    >
    > vector< vector< data_foto > >vector_fotos;
    >
    > And I want to access the fields of the struct for doing an assignement
    > in a case. I do this in a piece of my code:
    >
    > switch(pfc[j].id)
    > {
    > case CONTROL_HV:
    >
    > addPacketToSendBuffer( CONTROL_HV, 0, sizeof(T_ControlHV), (char *)
    > &h2pcnova->h2ig.cnt );
    > if (take_foto)
    > {
    > vector_fotos[minuto_actual][(int)(pfc[j].id)].id =
    > pfc[j].id;
    > vector_fotos[minuto_actual][(int)(pfc[j].id)].data
    > = (char*)&(h2pcnova->h2ig.cnt);
    > }
    >
    > [...]
    >
    > but when I debbug vector_fotos[minuto_actual] appears: "class
    > std::vector< data_foto, std::allocator< data_foto >&)0x0 Cannot
    > access"
    >
    > What's the problem??
    >


    I'd guess that whereas you've declared a
    vector< vector< data_foto > >vector_fotos;
    you haven't actually put any vector<data_foto> objects in it yet;

    In simpler terms:
    class Z {...};
    vector<Z> vector_zeds;
    vector_zeds[0].some_method(); // WRONG - NO SUCH ELEMENT
    vector_zeds.push_back(get_a_Z_from_somewhere());
    vector_zeds[0].some_method(); // OK now
     
    tragomaskhalos, Dec 19, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias =?ISO-8859-1?Q?K=E4ppler?=

    std::string::push_back vs. std::string::operator+=

    Matthias =?ISO-8859-1?Q?K=E4ppler?=, Nov 22, 2004, in forum: C++
    Replies:
    2
    Views:
    4,172
    Jonathan Mcdougall
    Nov 23, 2004
  2. Anonymous
    Replies:
    20
    Views:
    4,356
    Pete Becker
    Mar 30, 2005
  3. Jason Heyes
    Replies:
    8
    Views:
    737
    Andrew Koenig
    Jan 15, 2006
  4. Rune Allnor
    Replies:
    4
    Views:
    964
    Rune Allnor
    Dec 11, 2008
  5. mlt
    Replies:
    2
    Views:
    1,304
Loading...

Share This Page