struct one inside another
Jul 10, 2012 at 4:21pm UTC
Hi,
I'm trying to define two strut's one inside another and I'm doing like above but it no recognize tabela_e on struct nrotas, can someone advise me.
1 2 3 4 5 6 7 8 9 10
typedef struct {
string id_dest;
std::vector<std::vector<std::string> > caminhos;
}tabela_e;
typedef struct {
std::string id;
std::vector<tabela_e> rotas;
} nrotas;
if I want to to access to variables inside the struct I only have to do, for example, tabela_e.id_dest, right?
Regards,
CMarco
Jul 10, 2012 at 4:34pm UTC
1 2 3 4 5 6 7 8 9
struct tabela_e{
string id_dest;
std::vector<std::vector<std::string> > caminhos;
};
struct nrotas{
std::string id;
std::vector<tabela_e> rotas;
};
if I want to to access to variables inside the struct I only have to do, for example, tabela_e.id_dest, right?
First, create an object of type tabela_e
tabela_e x;
Then access it:
x.id_dest = "letters" ;
Last edited on Jul 10, 2012 at 4:36pm UTC
Jul 11, 2012 at 5:02pm UTC
Hi,
first thanks for the tips. So this is my struct inside another struct:
1 2 3 4 5 6 7 8 9 10 11
typedef struct tabela_e{
string id_dest;
std::vector<std::vector<std::string> > path;
}tabela_e;
typedef struct {
std::string id;
std::vector<tabela_e> route;
} nrotas;
nrotas mat;
tabela_e temp;
To put all the content that I want inside route I do:
mat.route.push_back(temp); // this goes all ok
My mat.route will be a three dimensional vector, How can I output his content.
I know that in a two dimensional I do:
1 2 3
for (size_t n = 0; n < temp.path.size(); n++)
for (size_t m = 0; m < temp.path[n].size(); m++)
EV << "Vector temp.path: " << temp.path[n][m] << '\n' ;
Can someone advise me of, how can I see the output content of my three dimensional vector named mat.route.
Regards,
CMarco
Last edited on Jul 11, 2012 at 5:04pm UTC
Jul 12, 2012 at 8:20am UTC
one tip?
Jul 12, 2012 at 9:28am UTC
route is not a 3D vector. It is a 1D vector. It contains objects that themselves contain a 2D vector.
string someString = mat.route[i].path[j][k];
Also, stop using typedef for struct
s. This is C++.
Jul 12, 2012 at 10:46am UTC
Hi,
thanks, I was saying the same but my colleges or saying no its a 3D vector... Now they are thinking twice...
If i remove typedef from my struct's I have an Eclipse CDT wrongly errors, and because of that i left the typedef.
To see the output content I do (it goes for someone o need):
1 2 3 4 5 6 7 8 9
for (size_t i = 0; i < mat.route.size(); i++){
temp=mat.route[i];
EV << "i " << i << '\n' ;
for (size_t n = 0; n < temp.path.size(); n++){
for (size_t m = 0; m < temp.path[n].size(); m++){
EV << "Dados: " << mat.route[i].path[n][m] << '\n' ;
}
}
}
Regards,
CMarco
Jul 12, 2012 at 10:55am UTC
When you removed the typedef, did you remember to put the name of the struct at the front?
C code:
1 2 3
typedef struct {
// some code
} tabela_e;
C++ code:
1 2 3 4
struct tabela_e
{
// some code
};
You wouldn't do this:
1 2 3
typedef class {
// some code
} tabela_e;
so why do it with struct?
Last edited on Jul 12, 2012 at 10:59am UTC
Jul 12, 2012 at 11:26am UTC
Hi,
I have the same errors using for example
boost::algorithm::split(vect, srctmp, boost::algorithm::is_any_of("-" ))
but I think is an Eclipse CDT error, because when build in console goes all ok.
I do with struct because I was advise that is the best way to do. But if you have some suggestion I will be glade to hear.
Regards
Jul 12, 2012 at 11:36am UTC
My suggestion is to do what I said above.
typedef struct
is a C kludge to save having to type the word "struct" many times. That's it. C++ does not need the kludge. In C++, define the struct:
1 2 3 4
struct someStruct
{
// code
};
and then just use it like any other proper object type
someStruct a;
Last edited on Jul 12, 2012 at 11:36am UTC
Topic archived. No new replies allowed.