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??