By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Definition order of POD

P: n/a
I am generating a bunch of class type PODs from an xml file.The thing
is,xml file has no notion of order of definitions, so I end up in
situations like;
struct a
{
b bval;

};
struct b
{
int aval;
};
which wont compile because b is not yet defined when the compiler hits
it.
I also dont want to split them into different files because there are
numerous
and also like to provide to clients a single point of entry.is there a
way to
avoid or circumvent this situation lest I should have to modify the
xml file itself?
Any help would be greatly appreciated....

Mar 16 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Mar 15, 7:44 pm, "hurcan solter" <hsol...@gmail.comwrote:
I am generating a bunch of class type PODs from an xml file.The thing
is,xml file has no notion of order of definitions, so I end up in
situations like;

struct a
{
b bval;

};

struct b
{
int aval;};

which wont compile because b is not yet defined when the compiler hits
it.
I also dont want to split them into different files because there are
numerous
and also like to provide to clients a single point of entry.is there a
way to
avoid or circumvent this situation lest I should have to modify the
xml file itself?
Any help would be greatly appreciated....
You might employ some sort of topological sort (cf. <http://
en.wikipedia.org/wiki/Topological_sortand <http://www.ddj.com/blog/
cppblog/archives/2006/10/abstraction_and.html>).

Cheers! --M

Mar 16 '07 #2

P: n/a
hurcan solter wrote:
I am generating a bunch of class type PODs from an xml file.The thing
is,xml file has no notion of order of definitions, so I end up in
situations like;
struct a
{
b bval;

};
struct b
{
int aval;
};
which wont compile because b is not yet defined when the compiler hits
it.
Yes, that code is invalid.
I also dont want to split them into different files because there are
numerous
and also like to provide to clients a single point of entry.
Even if you split them, you still need 'b' to be defined when 'a' is
being processed.
is there a
way to
avoid or circumvent this situation lest I should have to modify the
xml file itself?
Circumvent what exactly? The code as it stands, is invalid. There
is nothing that can be done to it, short of rewriting it to make it
valid.

How are you "generating" those PODs? Is that your C++ program or is
that something else, like a Perl script or a Python program? We can
only help with C++ code. When you generate those, you could try to
write the definition of 'a' and check if 'b' has been defined, and
if it hasn't, hold putting the definition of 'a' out until 'b' is
defined. But then you would be writing part of the compiler.

Fixing the program that produces your XML may actually be a more
viable solution.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 16 '07 #3

P: n/a

Even if you split them, you still need 'b' to be defined when 'a' is
being processed.
I thought it was obvious that i will generate necessary "#include"s
if i go that way.with some include guards it should be fairly
simple
and straightforward.
Circumvent what exactly? The code as it stands, is invalid. There
is nothing that can be done to it, short of rewriting it to make it
valid.

How are you "generating" those PODs? Is that your C++ program or is
that something else, like a Perl script or a Python program? We can
only help with C++ code. When you generate those, you could try to
write the definition of 'a' and check if 'b' has been defined, and
if it hasn't, hold putting the definition of 'a' out until 'b' is
defined. But then you would be writing part of the compiler.

Fixing the program that produces your XML may actually be a more
viable solution.
I was hoping for some kind of compiler magic I suppose.I'll give
the topological sorting idea a go, thanks for the input though .
cheers

Mar 16 '07 #4

P: n/a
On Mar 16, 8:44 am, "hurcan solter" <hsol...@gmail.comwrote:
>
struct a
{
b bval;

};

struct b
{
int aval;};

which wont compile because b is not yet defined when the compiler hits
it.
struct b;
struct a
{
b bval;

};

struct b
{
int aval;};

seems this compile.

Mar 16 '07 #5

P: n/a
"Sarath" <CS*****@gmail.comwrote in message
news:11**********************@y66g2000hsf.googlegr oups.com...
: On Mar 16, 8:44 am, "hurcan solter" <hsol...@gmail.comwrote:
: >
: struct a
: {
: b bval;
: >
: };
: >
: struct b
: {
: int aval;};
: >
: which wont compile because b is not yet defined when the compiler
hits
: it.
:
: struct b;
: struct a
: {
: b bval;
:
: };
:
: struct b
: {
: int aval;};
:
: seems this compile.

No, it is not valid C++.
It would only compile with a forward-declaration
if a's member was a pointer: b* bval;

A topological sort based on the dependency graph
is the obvious solution...
(hoping that there are no cyclic dependencies,
in which case the use of pointes would be required).
Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

Mar 16 '07 #6

P: n/a
"hurcan solter" <hs*****@gmail.comwrote in message
news:11**********************@y66g2000hsf.googlegr oups.com...
>I am generating a bunch of class type PODs from an xml file.The thing
is,xml file has no notion of order of definitions, so I end up in
situations like;
struct a
{
b bval;

};
struct b
{
int aval;
};
which wont compile because b is not yet defined when the compiler hits
it.
I also dont want to split them into different files because there are
numerous
and also like to provide to clients a single point of entry.is there a
way to
avoid or circumvent this situation lest I should have to modify the
xml file itself?
Any help would be greatly appreciated....
If you can use a poiner to b, then you could predefine the clase
class b;
struct a
{
b* bval;
}

However, if you need an instance of b (as your code is showing) then b has
to be defined before a. The easiest way, just change the order your are
declaring your classes. Cut and paste and make it
struct b
{
int aval;
};
struct a
{
b bval;
};
and it will compile.
Mar 16 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.