Answered by:
Regarding forward declaration of structs in C++

Question
-
Hello,
This is my first time working on C++ and I have some problems that needed some help please.
I have some sort of circular dependency among my structs. They look like this,
struct IF { string condition }; struct Stmt { IF ifStat; }; struct StmtLst { vector<Stmt> stmt; } struct IF { string condition; StmtLst thenPart; StmtLst elsePart; };
My IF struct needs to use the StmtLst struct so I had to declare it after the StmtLst struct. But, at the same time the stmt struct would require my IF struct to be declared before it.
I tried declared it as
struct IF IF;
to replace the first IF struct because I don't need it. I need the 2nd IF struct, but it's an error saying it uses an undefined struct .
Please help me. I'm total clueless what to do now. Thanks!
Saturday, February 5, 2011 7:12 PM
Answers
-
Mr Bel wrote:
I have some sort of circular dependency among my structs. They look like this,
struct IF { string condition }; struct Stmt { IF ifStat; }; struct StmtLst { vector<Stmt> stmt; } struct IF { string condition; StmtLst thenPart; StmtLst elsePart; };
You might be able to get away with this:
struct Stmt; struct StmtLst { vector<Stmt> stmt; }; struct IF { string condition; StmtLst thenPart; StmtLst elsePart; }; struct Stmt { IF ifStat; };
Formally, a parameter of std::vector must be a complete type, but I believe MSVC implementation accepts incomplete types.
If that doesn't work, then you'll have to hold something by pointer, to break the circle. E.g.
struct IF; struct Stmt { IF* ifStat; }; struct StmtLst { vector<Stmt> stmt; }; struct IF { string condition; StmtLst thenPart; StmtLst elsePart; };
Igor Tandetnik
- Marked as answer by Yi Feng Li Tuesday, February 15, 2011 2:26 AM
Saturday, February 5, 2011 7:49 PM
All replies
-
Mr Bel wrote:
I have some sort of circular dependency among my structs. They look like this,
struct IF { string condition }; struct Stmt { IF ifStat; }; struct StmtLst { vector<Stmt> stmt; } struct IF { string condition; StmtLst thenPart; StmtLst elsePart; };
You might be able to get away with this:
struct Stmt; struct StmtLst { vector<Stmt> stmt; }; struct IF { string condition; StmtLst thenPart; StmtLst elsePart; }; struct Stmt { IF ifStat; };
Formally, a parameter of std::vector must be a complete type, but I believe MSVC implementation accepts incomplete types.
If that doesn't work, then you'll have to hold something by pointer, to break the circle. E.g.
struct IF; struct Stmt { IF* ifStat; }; struct StmtLst { vector<Stmt> stmt; }; struct IF { string condition; StmtLst thenPart; StmtLst elsePart; };
Igor Tandetnik
- Marked as answer by Yi Feng Li Tuesday, February 15, 2011 2:26 AM
Saturday, February 5, 2011 7:49 PM -
Mr Bel wrote:
I have some sort of circular dependency among my structs. They look like this,
struct IF { string condition }; struct Stmt { IF ifStat; }; struct StmtLst { vector<Stmt> stmt; } struct IF { string condition; StmtLst thenPart; StmtLst elsePart; };
You might be able to get away with this:
struct Stmt; struct StmtLst { vector<Stmt> stmt; }; struct IF { string condition; StmtLst thenPart; StmtLst elsePart; }; struct Stmt { IF ifStat; };
Formally, a parameter of std::vector must be a complete type, but I believe MSVC implementation accepts incomplete types.
If that doesn't work, then you'll have to hold something by pointer, to break the circle. E.g.
struct IF; struct Stmt { IF* ifStat; }; struct StmtLst { vector<Stmt> stmt; }; struct IF { string condition; StmtLst thenPart; StmtLst elsePart; };
Igor Tandetnik
OMG!!
That solved it! I cracked my head for one whole day and still stucked.
You're good! Thanks a lot!!!!! Appreciate it very much.
Saturday, February 5, 2011 9:36 PM