structs overloading ?
Feb 5, 2011 at 6:02pm UTC
Hi,
May I ask for some help here please? I'm new to C++. I just tried googling but after a day I still can't solve my problem.
I have these structs declaration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
struct IF {
string condition
};
struct Stmt {
IF ifStat;
};
struct StmtLst {
vector<Stmt> stmt;
}
struct IF {
string condition;
StmtLst thenPart;
StmtLst elsePart;
};
Is it possible to overload a struct declaration?
Initially I wanted to declare just the 2nd IF struct but I can't because I had to declare it after the StmtLst struct.
But at the same time I have to declare the IF struct before the Stmt struct. and the Stmt must be before the StmtLst struct.
I tried using friend, union but to no avail.
I'm going crazy :( Please direct me the right way to deal with this. Thanks alot!!
Feb 5, 2011 at 6:11pm UTC
You need to forward declare a class.
Notice that you can't have two classes holding complete objects of each other or it will require infinite memory
eg:
Stmt has an IF object which has (at least) a Stmt object which has an IF object ...
You need to use references or pointers
Feb 5, 2011 at 6:56pm UTC
Can I do like this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
struct IF IF;
struct Stmt {
IF ifStat;
};
struct StmtLst {
vector<Stmt> stmt;
}
struct IF {
string condition;
StmtLst thenPart;
StmtLst elsePart;
};
How can I use pointers in here? Is it struct *mystruct;
mystruct { ... };
??
Thanks!
Last edited on Feb 5, 2011 at 6:57pm UTC
Topic archived. No new replies allowed.