Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   std:: vector push_back a struct (http://www.velocityreviews.com/forums/t560834-std-vector-push_back-a-struct.html)

jmsanchezdiaz 12-13-2007 04:23 PM

std:: vector push_back a struct
 
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.

The Last Ottoman 12-13-2007 05:29 PM

Re: std:: vector push_back a struct
 
On Dec 13, 6:23 pm, jmsanchezdiaz <jmsanchezd...@gmail.com> 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

LR 12-13-2007 05:57 PM

Re: std:: vector push_back a struct
 
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



Daniel T. 12-13-2007 08:41 PM

Re: std:: vector push_back a struct
 
jmsanchezdiaz <jmsanchezdiaz@gmail.com> 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 ) );
}

jmsanchezdiaz 12-18-2007 11:59 AM

Re: std:: vector push_back a struct
 
On 13 dic, 21:41, "Daniel T." <danie...@earthlink.net> wrote:
> jmsanchezdiaz <jmsanchezd...@gmail.com> 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

James Kanze 12-19-2007 08:35 AM

Re: std:: vector push_back a struct
 
On Dec 18, 12:59 pm, jmsanchezdiaz <jmsanchezd...@gmail.com> wrote:
> On 13 dic, 21:41, "Daniel T." <danie...@earthlink.net> wrote:
> > jmsanchezdiaz <jmsanchezd...@gmail.com> 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:james.kanze@gmail.com
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

tragomaskhalos 12-19-2007 03:29 PM

Re: std:: vector push_back a struct
 
On Dec 18, 11:59 am, jmsanchezdiaz <jmsanchezd...@gmail.com> wrote:
> On 13 dic, 21:41, "Daniel T." <danie...@earthlink.net> wrote:
>
>
>
>
>
> > jmsanchezdiaz <jmsanchezd...@gmail.com> 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



All times are GMT. The time now is 08:50 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.